JavaScript uses regular expressions to replace the content in a string

Source: Internet
Author: User
This article describes how to replace the content in a string with a regular expression in JavaScript, and provides a brief comment for ease of understanding. It has a certain reference value. If you need a friend, you can read more. Please refer to the specific implementation code.

// Cut the 'is: var str = 'Is all there is' from the string 'is this all there Is'; var subStr = new RegExp ('ais '); // create a regular expression object var result = str. replace (subStr, ""); // replace 'is with an empty string console. log (result); // Is th all there is var subStr = new RegExp ('is', 'I'); // create a regular expression object, case Insensitive var result = str. replace (subStr, ""); // replace 'is with an empty string console. log (result); // this all there is var subStr = new RegExp ('is', 'ig '); // create a regular expression object, case insensitive, global Search var result = str. replace (subStr, ""); // replace 'is with an empty string console. log (result); // th all there var subStr =/is/ig; // create a regular expression object by using the direct method, which is case-insensitive. Global Search for var result = str. replace (subStr, ""); // replace 'is with an empty string console. log (result); // th all there console. log (str); // Is this all there is visible replace does not change the original str


The above is all the content of this article. I hope this article will help you in your study or work, and I also hope to support PHP!

For more articles about how JavaScript uses regular expressions to replace the content in a string, refer to PHP!

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.