In JavaScript regular expressions are represented by RegExp class and integrated into String.
String methods
str.match(regexp) {#str-match}
For a global regexp returns array of matches:
alert( "12 34".match( /\d+/g ) ) // 12, 34
g flag - returns the first match and groups:
alert( "12 34".match( /(\d)(\d)/ ) ) // 12, 1, 2
The str.match is unable to find all matches with groups. There is another method for that, regexp.exec, given below.
Another subtle feature is that when there are no results, match returns null, not an empty array.
Keeping that in mind helps to evade errors like below:
var matches = "lala".match( /\d/ ) alert(matches.length) // error! match is null, there is no length
str.search(regexp) {#str-search}
Returns the index of first match, or -1 if not found:
alert( "test12".search( /\d+/ ) ) // 4
Can be used to test a string for a match:
var str = prompt("Enter a string", 'test12')
if (str.search(/\d/) != -1) {
alert("The string has numbers")
} else {
alert("The string has no numbers")
}
str.split(regexp|substr, limit) {#str-split}
Splits a string into array by given substr or regexp.
For example:
alert( '12-34-56'.split('-') ) // 12, 34, 56
We can also use a regexp:
alert( 'Hello \n world'.split( /\s+/ ) ) // Hello, world
If there is an optional second argument limit, the array is cut at limit length:
alert( '12-34-56'.split('-', *!*2*/!*) ) // 12, 34
So, the part of split after limit pieces is lost.
str.replace(regexp|substr, newSubStr|function)
A swiss army knife for replacements. At it’s simplest form accepts a string as the first argument (what to search).
alert( 'aaa'.replace('a','b') ) // baa
Only the first occurence is replaced for a string. To replace all occurences, the first argument must be a global regexp.
alert( 'aaa'.replace( /a/g ,'b') ) // bbb
Because of that, replace is rarely used with a string as the first argument.
The second argument can be a replacement string with following special chars possible:
| Pattern | Inserts |
|---|---|
$$ |
Inserts a “$”. |
$& |
Inserts the matched substring. |
$``</td> <td>Inserts the portion of the string that precedes the matched substring.</td> </tr> <tr> <td>$’</td> <td>Inserts the portion of the string that follows the matched substring.</td> </tr> <tr> <td style="white-space: nowrap;"><code>$n</code> or <code>$nn</code></td> <td>Where <code>n</code> or <code>nn</code> are decimal digits, inserts the nth parenthesized submatch string, provided the first argument was aRegExpobject.</td> </tr>str, then variable count ofp1, p2, .. pn, thenoffset, s:
str- the full match,</li>p1, p2, …, pn- captured bracket groups, numbered left-to-right</li>offset- the position of the match,</li>s- the source string</li>regexp.exec(str)is exactly the same asstr.match(regexp). It returns the first match and groups.
regexp.exec(str)is the only way to find all matches with groups.
re.execmultiple times and each time gives a new match (ornull).
RegExpobject has internal propertylastIndex.
execstarts searching fromlastIndexposition, and then moves it forward (or resets if not found).
lastIndex = 0at start, then it shifts on each match, and finally, when there are no more matches, is set back to0.
regexpmatches thestr. Returnstrue/false.
str.split</a>.</li>str.replace</a>.</li>
regexp.exec</a>, without groups - can use a single call to <a href="#str-match">str.match</a>.</li>
regexp.test`.
|
- https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/match
- https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace
- https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/search
- https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/RegExp/exec
- https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/RegExp/test