The character replace () usage in the JS replacement string

Source: Internet
Author: User

Such as

The code is as follows Copy Code

var str = "Everybody is a man, isn't he?"
str = str.replace (' Yes ', ' well ');
alert (str);


Only the first "?" is replaced in the above code, and the output is "Everyone is a man, isn't it?"

Replacing a regular expression is a good idea if you want to replace all eligible content in a string, and you can replace it with the following code

The code is as follows Copy Code

var str= ' Everyone is a man, isn't it? '
str = str.replace (//g, ' well ');
alert (str);


This will output "Everyone is a man, is not it?", all eligible content has been replaced, particularly to note that the regular expression//g Here's G, this g represents the global, does not give this configuration item is not all replaced.

Cases

The code is as follows Copy Code

<script type= "Text/javascript" >
var str= "Www.111cn.netaa"
document.write (Str.replace (/aa/, ""))//output result is www.111cn.net
</script>

Cases

  code is as follows copy code

//ensure word " JavaScript "is correctly case-sensitive
Text.replace (/javascript/i, ' JavaScript ');
////Replace all double quotes with a pair of front and back single quotes
Text.replace ([^] "/g," "'" ");
//Converts a separate name from the format "Mack, Xu" to "Xu Mack"
Name.replace (/(w+) s*,s* (w+)/, "$ $");
//capitalize the first letter of all words in a string
Text.replace (/bw+b/g, function (word) {
    return word.substring (0, 1). toUpperCase () + Word.substring (1);
});

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.