1. Regular Expressions in strings
you can use regular expressions to find, extract, split, and replace strings. Some of the following special methods are provided in the string class:
Boolean matches (string regex): Determines whether the string matches the specified regular expression.
String ReplaceAll (string regex, String replacement): Replaces all substrings in the string that match the regex to replacement.
String[] Split (string regex): Use a regex as a separator, dividing the string into multiple substrings.
These special methods depend on the regular expressions provided by Java.
2. Creating regular Expressions
x: Character x (x can represent any valid character);
\0mnn: The character represented by the octal number omnn;
\XHH: The character represented by hexadecimal 0xhh;
\uhhhh: Unicode character as represented by hexadecimal 0xhhhh;
\ t: Tabs (' \u0009 ');
\ n: New lines (line break) (' \u000a ');
\ r: Return character (' \u000d ');
\f: Page break (' \u000c ');
\a: Alarm (Bell) character (' \u0007 ');
\e:escape character (' \u001b ');
\cx:x the corresponding control character. For example, \cm matches ctrl-m. x value must be one of a~z or a~z;
3. Special characters in regular expressions
$: Matches the end of a row. To match the $ character itself, please use \$;
^: Matches the beginning of a line. To match the ^ character itself, please use \^;
(): Marks the start and end position of a subexpression. To match these characters, use \ (and \);
[]: Used to determine the start and end position of the bracket expression. To match these characters, use \[and \];
{}: The frequency used to mark the appearance of a front-face expression. To match these characters, use \{and \};
*: Specifies that the front face expression can appear 0 or more times. To match the * character itself, use \*;
+: Specifies that the front face expression can occur one or more times. to match the + character itself, please use \+;
?: Specifies that the front face expression can appear 0 or more times. To match the character itself, please use \?;
.: matches any unit character except \ n of a newline character. To match, the character itself, please use \.;
\: Used to escape the next character, or to specify octal, hexadecimal characters. To match \ characters, use \ \;
|: Specify an optional item between the two items. to match the | character itself, use \|;
4. Predefined characters
.: can match any character;
\d: Match all numbers of 0~9;
\d: matching non-digits;
\s: Matches all whitespace characters, including spaces, tabs, carriage returns, page breaks, line breaks, etc.
\s: Matches all non-white space characters;
\w: Matches all the word characters, including 0~9 all numbers, 26 letters and underscores (_);
\w: Match all non word characters;
5. Boundary matching character
^: The beginning of a line
$: End of line
\b: The boundary of a word
\b: Non-word boundaries
\a: The beginning of the input
\g: End of previous match
\z: The end of the input, used only for the last terminator
\z: End of input
6. Symbols that indicate the number of matches
The following illustration shows the symbols that represent the number of matches that are used to determine the number of occurrences of the symbol immediately to the left of the symbol:
(1) Suppose we want to search the U.S. Social Security number in a text file. The format of this number is 999-99-9999. The regular expression used to match it is shown in figure one. In a regular expression, a hyphen ("-") has a special meaning, which represents a range, for example, from 0 to 9. Therefore, when matching the hyphenation symbol in the social security number, it is preceded by an escape character "\".
(2) Suppose that when searching, you want the hyphen to appear or not to appear-that is, 999-99-9999 and 999999999 are in the correct format. At this point, you can add the word "? The quantity qualifier symbol, as shown in the figure:
(3) Let's look at another example here. A format for U.S. car licences is four digits plus two letters. Its regular expression is preceded by the number part "[0-9]{4}", plus the letter part "[A-z]{2}". The following illustration shows the complete regular expression.
7. Some examples
Example 1
function replace (content) {
var reg = ' \\[(\\w+) \] ', pattern
= new RegExp (Reg, ' G ');
return Content.replace (Pattern, ' ');
}
or
function replace (content) {return
content.replace/\[(\w+) \/g, ' ');
Example 2
Zero-width Look behind replacement scheme
//(? <= ...) and (?<!...)
Method One: Reverse the string, search with lookahead, replace and then pour back, for example:
String.prototype.reverse = function () {return
this.split ("). Reverse (). Join (');
Simulate ' Foo.bar|baz '. Replace (/(? <=\.) b/, ' C ' will be preceded by '. ' b replaced by C
' Foo.bar|baz '. Reverse (). replace (/b (? =\.) /g, ' C '). Reverse ()//foo.car|baz
//Method Two: Do not use 0 wide assertion, own Judge
//Simulate ' foo.bar|baz '. Replace (/(? <=\.) b/, ' C ' will be preceded by '. ' B to C
' Foo.bar|baz '. Replace (/(\.)? b/, function ($, $) {return
$ $ + ' C ': $
})//foo.car|baz
//Simulate ' foo.bar|baz '. Replace (/(? <!\.) b/, ' C ' will not be preceded by '. ' B to C
' Foo.bar|baz '. Replace (/(\.)? b/, function ($, $) {return
$ $: ' C ';
})//foo.bar|caz
//This method is useful in some simpler scenarios and can be used with lookahead
//But there are also many scenarios that are not valid, such as:
//' TTTT '. Replace (/(? <=t) t/g, ' x ') The result should be ' txxx '
tttt '. Replace (/(t)? t/g, Function ($, $) {return
$ $ + ' x ': $
})//TXTX
Example 3
The use of $& symbols
function Escaperegexp (str) {return
str.replace (/[abc]/g, "($&)");
}
var str = ' a12b34c ';
Console.log (Escaperegexp (str)); (a) (b) (c)