JavaScript Replace method and regular expression combined with application

Source: Internet
Author: User
Tags regular expression

Hello everyone!! Tonight in China soft g43* dormitory Nothing to do, the JavaScript to replace the method to explain, if the wrong or unreasonable is reasonable to do, because I am not a veteran, nor rookie, I do not know what the bottom of my bird?? Ah ~ ~

The syntax for the Replace method is: Stringobj.replace (rgexp, ReplaceText) stringobj is a string, reexp can be a regular expression object (REGEXP), or it can be a string ( String), ReplaceText is an alternative to the found string ... To help you get a better understanding, here's a simple example to illustrate

<script language="javascript">
var stringObj="终古人民共和国,终古人民";
//替换错别字“终古”为“中国”
//并返回替换后的新字符
//原字符串stringObj的值没有改变
var newstr=stringObj.replace("终古","中国");
alert(newstr);
</script>

I am smarter than you, after reading the example above, you will find that the second typo "end ancient" has not been replaced by "China", we can perform two replace method of the second typo "end ancient" also replaced, the program after the improvement is as follows:

<script language="javascript">
var stringObj="终古人民共和国,终古人民";
//替换错别字“终古”为“中国”
//并返回替换后的新字符
//原字符串stringObj的值没有改变
var newstr=stringObj.replace("终古","中国");
newstr=newstr.replace("终古","中国");
alert(newstr);
</script>

We can carefully think about, if there are N of N times a typo, is not also to perform N-second side replace method to replace the typo?? Oh, don't be afraid, there is a regular expression after the use of a typo to perform a replace method. After the program has been improved, the following code

<script language="javascript">
var reg=new RegExp("终古","g"); //创建正则RegExp对象
var stringObj="终古人民共和国,终古人民";
var newstr=stringObj.replace(reg,"中国");
alert(newstr);
</script>

The above is about the simplest application of the Replace method, do not know whether we understand?? Let's start with a slightly more complex application.

When you search for articles on some sites, you will find a phenomenon, that is, the search keyword will highlight the color display?? How does this happen?? In fact, we can use regular expressions to achieve, specifically how to achieve it? The simple principle, see the code below

<script language="javascript">
var str="中华人民共和国,中华人民共和国";
var newstr=str.replace(/(人)/g,"<font color=red>$1</font>");
document.write(newstr);
</script>

The above procedure lacks the interactivity, we improve the program, realizes can enter the character which to look for independently

<script language="javascript">
var s=prompt("请输入在查找的字符","人");
var reg=new RegExp("("+s+")","g");
var str="中华人民共和国,中华人民共和国";
var newstr=str.replace(reg,"<font color=red>$1</font>");
document.write(newstr);
</script>

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.