About the Replace_ basics of string in JavaScript

Source: Internet
Author: User

The replace () method replaces some characters in a string with some other characters, or replaces a substring that matches a regular expression.

Syntax: String.Replace (substr/reg,replacestr/function)

The first argument can be a substring of a string, or it can be a regular expression, the second argument can be a string or a processing method, and we'll look at

Copy Code code as follows:

document.write (' 1234 '. Replace (1, ' X ')); we can get results: X234, it's normal, but

Copy Code code as follows:

document.write (' 1214 '. Replace (1, ' X ')); we expected the result to be: x2x4, but the result is: X214, that is, if our first argument writes a substring, Then replace will stop the search only by replacing the first match

We're going to change it to regular.
Copy Code code as follows:

document.write (' 1214 '. Replace (/1/g, ' X ')); this time we can get the desired result: x2x4

Let's look at the function.
Copy Code code as follows:

var r = ' ABCD '. Replace (/\w/g, function () {
Return ' X ';
});
document.write (R);

At this point we can see the expected result: XXXX, all characters are replaced with X, this is my previous understanding of replace, but I see this example in the JavaScript language pristine, I am puzzled

Copy Code code as follows:

var t = document.getElementById (' t ');
String.prototype.deentityfy = function () {
var entity = {
quot: ' "',
LT: ' < ',
GT: ' > '
};
return function () {
Return This.replace (/&) ([^&;] +);/g, function (A, b) {
var r = Entity[b];
return typeof r = = ' String '? r:a;
}); End of Replace
};
} ();

document.write (' < ' > '. Deentityfy ());


This code adds a Deentityfy method to the JavaScript string object to replace the HTML character in the string (replace with,< with <,> Replace with >), and we ignore the language techniques used by the author. See how his replace is used, the first parameter is a regular expression, is the match mentioned before the three string, the second parameter's function unexpectedly has two parameters, these two parameters in the end is what? Why does the method get the desired result, let's analyze it briefly.

First ENTITY[B] is the use of JavaScript associative arrays, based on the name of the array data to get value, to facilitate understanding, we might as well change this method to make it easier, so that we can see more clearly what the function parameters Also to eliminate the problem of browser transcoding, we modify the replacement string

Copy Code code as follows:

String.prototype.deentityfy = function () {
var entity = {
A: ' A ',
B: ' B ',
C: ' C '
};
return function () {
Return This.replace (/1 ([^12]) 2/g, function (A, b) {
for (var i = 0; i < arguments.length; i++) {
document.write (Arguments[i] + ' <br/> ');
}
document.write (' ===========================<br/> ');
var r = Entity[b];
return typeof r = = ' String '? r:a;
}); End of Replace
};
} ();
document.write (' 1a21b21c2 '. Deentityfy ());

So, we print out the parameters of the method and see what the result is.
Copy Code code as follows:

A2
A

A21b21c2
===========================
B2


A21b21c2
===========================
C2
C

A21b21c2
===========================
Abc

It's strange, right, the final < "> is the result of the method, very correct, get the expected result, let's look at the parameter part of the function,

The function is called 3 times, exactly the number of matches, and each time a matching string is replaced.

The method has four parameters each time it is invoked

The first argument is very simple, a matching string

The second one is weird, but it's not hard to see each one. The second argument is a match within the regular expression bracket

The third argument and the easy to think of is to match the index in the string

The fourth argument is the original string

It's amazing, isn't it, but that's it, let's write another try

Copy Code code as follows:

var r = ' 1a21b21c2 '. Replace (/1\w2/g, function () {
for (var i = 0; i < arguments.length; i++) {
document.write (Arguments[i] + ' <br/> ');
}
document.write (' ===========================<br/> ')
Return ' X ';
});
document.write (R);

Like the previous example, it simply replaces all occurrences with x to see the result

Copy Code code as follows:

A2

A21b21c2
===========================
B2

A21b21c2
===========================
C2

A21b21c2
===========================
Xxx


Unexpectedly right, the result is expected, but the parameter is one less, the second argument disappears to see what the difference is--the seemingly superfluous parentheses in the regular expression are missing, and in the previous example, the second argument is exactly the match within the parentheses, and is the second argument the match in parentheses in the regular expression? Let's add the parentheses back to verify

Copy Code code as follows:

var r = ' 1a21b21c2 '. Replace (/1 (\w2)/g, function () {
for (var i = 0; i < arguments.length; i++) {
document.write (Arguments[i] + ' <br/> ');
}
document.write (' ===========================<br/> ')
Return ' X ';
});
document.write (R);

Look at the results.

Copy Code code as follows:

A2
A2

A21b21c2
===========================
B2
B2

A21b21c2
===========================
C2
C2

A21b21c2
===========================
Xxx


Sure enough, so that we know exactly what parameters are in the function, now look at the example of the JavaScript language essence to understand that, of course, we need to know the associative array, immediately execute functions, closures and arguments objects, If we were to capitalize all the words in a sentence, would it be?

Copy Code code as follows:

A lot of ways, this is just to verify that our theory was deliberately written in such a cumbersome way
var sentence = ' I love you ';
var upper = Sentence.replace (/(\w) \w*\b/g, function (a,b) {
Return B.touppercase () +a.substring (1);
});
document.write (upper);

Copy Code code as follows:

Well, it's a job to write.
var sentence = ' I love you ';
var upper = Sentence.replace (/\w+\b/g, function (a) {
Return A.substr (0,1). toUpperCase () +a.substring (1);
});
document.write (upper);

Related Article

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.