RegExp (regular expression) object
来源:互联网 发布:工业机器人应用编程 编辑:程序博客网 时间:2024/04/29 10:25
两种实现方式:
1.Literal syntax:
//match all 7 digit numbers
var phonenumber= /\d{7}/
2.Dynamically, with the RegExp()
constructor:
//match all 7 digit numbers (note how "\d" is defined as "\\d")
var phonenumber=new RegExp("\\d{7}", "g")
\d
" must be defined as "\\d
". Pattern flags (switches)
Position Matching
Literals
"\50" matches left parentheses character "(" \xddMatches the ASCII character expressed by the hex number dd.
"\x28" matches left parentheses character "(" \uxxxxMatches the ASCII character expressed by the UNICODE xxxx.
"\u00A3" matches "£".
The backslash (\) is also used when you wish to match a special character literally. For example, if you wish to match the symbol "$" literally instead of have it signal the end of the string, backslash it: /\$/
Character Classes
NOTE: the caret used within a character class is not to be confused with the caret that denotes the beginning of a string. Negation is only performed within the square brackets.
/[^AN]BC/ matches "BBC" but not "ABC" or "NBC". .(Dot). Match any character except newline or another Unicode line terminator./b.t/ matches "bat", "bit", "bet" and so on. \wMatch any alphanumeric character including the underscore. Equivalent to [a-zA-Z0-9_]./\w/ matches "200" in "200%" \WMatch any single non-word character. Equivalent to [^a-zA-Z0-9_]./\W/ matches "%" in "200%" \dMatch any single digit. Equivalent to [0-9]. \DMatch any non-digit. Equivalent to [^0-9]./\D/ matches "No" in "No 342222" \sMatch any single space character. Equivalent to [ \t\r\n\v\f]. \SMatch any single non-space character. Equivalent to [^ \t\r\n\v\f].Repetition
Alternation & Grouping
RegExp.$1
,RegExp.$2
etc after the pattern itself is matched or compared. For example, the following matches "2 chapters" in "We read 2 chapters in 3 days", and furthermore isolates the value "2":var mystring="We read 2 chapters in 3 days"
var needle=/(\d+) chapters/
mystring.match(needle) //matches "2 chapters"
alert(RegExp.$1) //alerts captured subpattern, or "2"
The subpattern can also be back referenced later within the main pattern. See "Back References" below.
The following finds the text "John Doe" and swaps their positions, so it becomes "Doe John":"John Doe".replace(/(John) (Doe)/, "$2 $1")
(?:x)Matches x but does not capture it. In other words, no numbered references are created for the items within the parenthesis./(?:.d){2}/ matches but doesn't capture "cdad".x(?=y)Positive lookahead: Matches x only if it's followed by y. Note that y is not included as part of the match, acting only as a required conditon./George(?= Bush)/ matches "George" in "George Bush" but not "George Michael" or "George Orwell".
/Java(?=Script|Hut)/ matches "Java" in "JavaScript" or "JavaHut" but not "JavaLand".
x(?!y)Negative lookahead: Matches x only if it's NOT followed by y. Note that y is not included as part of the match, acting only as a required condiiton./^\d+(?! years)/ matches "5" in "5 days" or "5 oranges", but not "5 years".|Alternation combines clauses into one regular expression and then matches any of the individual clauses. Similar to "OR" statement./forever|young/ matches "forever" or "young"
/(ab)|(cd)|(ef)/ matches and remembers "ab" or "cd" or "ef".
Back references
/(hubba)\1/
is equivalent to the pattern /hubbahubba/, as "\1" is replaced with the value of the first subpattern within the pattern, or (hubba), to form the final pattern.
Lets say you want to match any word that occurs twice in a row, such as "hubba hubba." The expression to use would be:
/(\w+)\s+\1/
"\1" is replaced with the value of the first subpattern's match to essentially mean "match any word, followed by a space, followed by the same word again".
If there were more than one set of parentheses in the pattern string you would use \2 or \3 to match the desired subpattern based on the order of the left parenthesis for that subpattern. In the example:
/(a (b (c)))/
"\1" references (a (b (c))), "\2" references (b (c)), and "\3" references (c).
Regular Expression methods
Note: Also updates the $1…$9 properties in the RegExp object.
var oldstring="Peter has 8 dollars and Jane has 15"newstring=oldstring.match(/\d+/g)
//returns the array ["8","15"]RegExp.exec(string)Similar to String.match() above in that it returns an array of information or null if no match is found. Unlike String.match() however, the parameter entered should be a string, not a regular expression pattern.var match = /s(amp)le/i.exec("Sample text")
//returns ["Sample","amp"]String.replace(regular expression, replacement text)Searches and replaces the regular expression portion (match) with the replaced text instead. For the "replacement text" parameter, you can use the keywords $1 to $99 to replace the original text with values from subpatterns defined within the main pattern.
The following finds the text "John Doe" and swaps their positions, so it becomes "Doe John":
var newname="John Doe".replace(/(John) (Doe)/, "$2 $1")
The following characters carry special meaning inside "replacement text":
- $1 to $99: References the submatched substrings inside parenthesized expressions within the regular expression. With it you can capture the result of a match and use it within the replacement text.
- $&: References the entire substring that matched the regular expression
- $`: References the text that proceeds the matched substring
- $': References the text that follows the matched substring
- $$: A literal dollar sign
The "replacement text" parameter can also be substituted with a callback function instead. See example below.
var oldstring="(304)434-5454"newstring=oldstring.replace(/[\(\)-]/g, "")
//returns "3044345454" (removes "(", ")", and "-")String.split (string literal or regular expression)Breaks up a string into an array of substrings based on a regular expression or fixed string.var oldstring="1,2, 3, 4, 5"
newstring=oldstring.split(/\s*,\s*/)
//returns the array ["1","2","3","4","5"]String.search(regular expression)Tests for a match in a string. It returns the index of the match, or -1 if not found. Does NOT support global searches (ie: "g" flag not supported)."Amy and George".search(/george/i)
//returns 8RegExp.test(string)Tests if the given string matches the Regexp, and returns true if matching, false if not.var pattern=/george/i
pattern.test("Amy and George")
//retuns true
参考
1.http://www.javascriptkit.com/jsref/regexp.shtml
- RegExp (regular expression) object
- 正则表达式(RegExp Regular Expression)基础知识
- 正则表达式(Regular Expression,regex,regexp)
- regular expression
- Regular Expression
- regular expression
- Regular Expression
- Regular Expression
- regular expression
- Regular Expression
- Regular Expression
- regular expression
- Regular expression
- Regular Expression
- Regular Expression
- regular expression
- Regular expression
- regular expression
- 职来职往观后感
- windows安装cygwin
- Android开发耗电优化
- 记录程序员点滴,记录我的成长
- TCP三次握手/四次挥手详解
- RegExp (regular expression) object
- 这十月,与C++走过的梦
- hdfs硬盘中dfs.data.dir相关和一些说明
- OCR读书笔记--第三章 进程
- vc++2010代码格式化方法(使用GreatCode)
- Java书籍
- Oracle 用户、角色与权限管理
- iOS图标尺寸总结
- adroid setting获取部分系统属性