A search and replace instance of a JavaScript string

Source: Internet
Author: User
Tags constructor regular expression

The Replace (Regexp,replacement) method has two parameters, the first parameter can be a plain text string or a RegExp object, see the use of RegExp objects, and the second argument is a string or a function.

Here are some examples of JS string replacements:

If it is a normal replacement, you can only replace the first one, to replace all, you need to use a regular expression. The following code can tell the difference between the two:

The code is as follows Copy Code


<title>test</title>
<script language= "JavaScript" >
<!--
var s = "Testtest";

G in the second argument matches all, I means ignore case
var regs = new RegExp ("Test", "GI");

Alert (S.replace ("Test", "Hello")); Replace only one
Alert (S.replace (regs, "Hello")); Replace All
-->
</script>
<body>
</body>

Next we look at the example

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 $ $ years old, come from $");
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) {//can also use non explicit parameters, using arguments to obtain
name = P1;
age = p2;
from = P3;
Return "She's" +p1+ "," +p2+ "years old, come from" +P3;
}
var rep=info.replace (Reg, Prase_info);
Alert (rep);
Aler (name);

2, the use of RegExp objects

JavaScript provides a RegExp object to complete the operation and functionality of regular expressions, and each regular expression pattern corresponds to a regexp instance. There are two ways to create an instance of a RegExp object.

Using the explicit constructor of REGEXP, the syntax is: New REGEXP ("pattern" [, "Flags"]), using the implicit constructor of REGEXP, in plain text format:/pattern/[flags]. The two statements in Example 4 are equivalent.

Example 4:

The code is as follows Copy Code

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

3, the search of strings and the use of the Exec () method

The Exec () method returns an array that holds the result of the match. 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 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 string strings contain text that matches regexpobject, or false.

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 JS Replacement method


The return value is the string after the substitution operation.

Simple usage of string.replace ()

The code is as follows Copy Code
var text = "JavaScript is very powerful!" ";
Text.replace (/javascript/i, "JavaScript");

Back: JavaScript is very powerful!
String.Replace () Replaces all occurrences of the target character

The code is as follows Copy Code
var text= "JavaScript is very powerful! JAVASCRIPT is one of my favorite languages! ";
Text.replace (/javascript/ig, "JavaScript");
Back: JavaScript is very powerful! JavaScript is one of my favorite languages!

String.Replace () to implement the swap position

The code is as follows Copy Code
var name= "Doe, John";
Name.replace (/(w+) s*,s* (w+)/, "$ $");
Back: John Doe

String.Replace () implementation replaces all double quotes with characters enclosed in brackets

The code is as follows Copy Code
The var text = ' JavaScript ' is very powerful! ';
Text.replace ([^ "]*)"/g, "[$]");
Back: [JavaScript] very powerful!

String.Replace () capitalize all characters first letter

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.