In js, We need to search for strings and use indexOf and then replace (). In fact, we don't need to do this at all. We just need a simple positive table expression under replace.
The replace (regexp, replacement) method has two parameters. The first parameter can be a plain text string or a RegExp object. For details, see the use of the RegExp object; the second parameter can be a string or a function.
The following is an example of JS string replacement:
If it is a normal replacement, you can only Replace the first one. To replace all, you must use a regular expression. The following code shows the differences between the two:
| The Code is as follows: |
Copy code |
|
<Html>
<Head>
<Title> test </title>
<Script language = "JavaScript">
<! --
Var s = "testtest ";
// G in the second parameter indicates that all matches, And I indicates that case-insensitive
Var regS = new RegExp ("test", "gi ");
Alert (s. replace ("test", "Hello"); // replace only one
Alert (s. replace (regS, "Hello"); // replace all
// -->
</Script>
</Head>
<Body>
</Body>
</Html>
|
Next let's look at the instance
Example 1:
| The Code is as follows: |
Copy code |
|
Var str = "Hello world! ";
Document. write (str. replace (/world/, "phper ")); |
Example 2:
| The Code is as follows: |
Copy code |
|
Var reg = new RegExp ("(\ w +), (\ d +), (\ w +)", "gmi ");
Var info = "Lili, 14, China ";
Var rep = info. replace (reg, "She is $1, $2 years old, come from $3 ");
Alert (rep ); |
Example 3:
| The Code is as follows: |
Copy code |
Var reg = new RegExp ("(\ w +), (\ d +), (\ w +)", "gmi ");
Var info = "Lili, 14, China ";
Var name, age, from;
Function prase_info (m, p1, p2, p3) {// you can also use a non-explicit parameter, obtained using arguments
Name = p1;
Age = p2;
From = p3;
Return "She is" + p1 + "," + p2 + "years old, come from" + p3;
}
Var rep = info. replace (reg, prase_info );
Alert (rep );
Aler (name );
|
2. Use of RegExp objects
JavaScript provides a RegExp object to perform operations and functions on regular expressions. Each regular expression pattern corresponds to a RegExp instance. You can create an instance of the RegExp object in two ways.
Use RegExp's explicit constructor Syntax: new RegExp ("pattern" [, "flags"]); Use RegExp's implicit constructor in plain text format: /pattern/[flags]. In Example 4, the two statements are equivalent.
Example 4:
| The Code is as follows: |
Copy code |
|
Var re1 = new RegExp ("\ d {5 }");
Var re2 =/d {5 }/; |
3. string search and use of the exec () method
The exec () method returns an array containing matching results. If no match is found, the return value is null.
Example 5:
| The Code is as follows: |
Copy code |
|
Var reg = new RegExp ("(\ w +), (\ d +), (\ w +)", "gmi ");
Var m1_reg.exe c ("Lili, 14, China ");
Var s = "";
For (I = 0; I <m. length; I ++ ){
S = s + m [I] + "n ";
}
Alert (s ); |
4. Use of the test () method
RegExpObject. test (string)
If the string contains the text that matches RegExpObject, true is returned; otherwise, false is returned.
Example 6:
| The Code is as follows: |
Copy code |
|
Var reg = new RegExp ("(\ w +), (\ d +), (\ w +)", "gmi ");
Var m = reg. test ("Lili, 14, China ");
Alert (RegExp. $1 );
Alert (RegExp. $2 );
Alert (RegExp. $3 ); |
Add the js replacement method.
The return value is the string after the replacement operation is executed.
String. replace ()
| The Code is as follows: |
Copy code |
Var text = "javascript is very powerful! ";
Text. replace (/javascript/I, "JavaScript ");
|
// Return: JavaScript is very powerful!
String. replace () replace all Target Characters
| The Code is as follows: |
Copy code |
Var text = "javascript is very powerful! JAVASCRIPT is my favorite language! ";
Text. replace (/javascript/ig, "JavaScript ");
// Return: JavaScript is very powerful! JavaScript is my favorite language!
|
String. replace ()
| The Code is as follows: |
Copy code |
Var name = "Doe, John ";
Name. replace (/(w +) s *, s * (w +)/, "$2 $1 ");
// Return value: John Doe
|
String. replace () is used to replace all characters in double quotation marks with characters in brackets.
| The Code is as follows: |
Copy code |
Var text = '"JavaScript" is very powerful! ';
Text. replace (/"([^"] *) "/g," [$1] ");
// Return: [JavaScript] is very powerful!
|
String. replace () uppercase letters
| The Code is as follows: |
Copy code |
Var text = 'a journey of a thousand miles begins with single step .';
Text. replace (/bw + B/g, function (word ){
Return word. substring (0, 1). toUpperCase () +
Word. substring (1 );
});
// Return: A Journey Of A Thousand Miles Begins With Single Step.
|