Home > Web Front-end > JS Tutorial > body text

JS uses the replace() method and regular expressions to search and replace strings_javascript skills

WBOY
Release: 2016-05-16 16:52:55
Original
1609 people have browsed it

1. Replacement of JS strings and use of replace() method

The replace(regexp,replacement) method has two parameters. The first parameter can be a plain text string or a RegExp object. For details, please see the use of RegExp objects; the second parameter can be a string or a RegExp object. a function.

The following is an example of JS string replacement:

Example 1:

Copy code The code is as follows:

var str="Hello world !";
document.write(str.replace(/world/, "phper"));

Example 2:
Copy code The code is as follows:

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:
Copy code The code is as follows:

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 non-explicit parameters and use arguments to obtain
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 object

JavaScript provides a RegExp object to complete operations and functions related to regular expressions. Each regular expression pattern corresponds to a RegExp instance. There are two ways to create instances of RegExp objects.

Use the explicit constructor of RegExp, the syntax is: new RegExp("pattern"[,"flags"]); use the implicit constructor of RegExp, in plain text format: /pattern/[flags]. The two statements in Example 4 are equivalent.

Example 4:

Copy code The code is as follows:

var re1 = new RegExp( "\d{5}");
var re2 = /d{5}/;

3. String search and use of exec() method

The exec() method returns an array, which stores the matching results. If no match is found, the return value is null.

Example 5:

Copy code The code is as follows:

var reg=new RegExp( "(\w ),(\d ),(\w )","gmi");
var m=reg.exec("Lili,14,China");
var s="";
for (i = 0; i < m.length; i ) {
s = s m[i] "n";
}
alert(s);

4. Use of test() method

RegExpObject.test(string)

Returns true if the string string contains text matching RegExpObject, otherwise returns false.

Example 6:

Copy code The code is as follows:

var reg=new RegExp( "(\w ),(\d ),(\w )","gmi");
var m=reg.test("Lili,14,China");
alert(RegExp.$1) ;
alert(RegExp.$2);
alert(RegExp.$3);
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!