JS Regular expression Replace has variables in the solution to use the RegExp class

Source: Internet
Author: User

have been more afraid to use regular expressions, seemingly very abstruse and complex appearance, so in the use of JS to manipulate strings, I use the most is replace, split, substring, indexof, and other functions, these functions sometimes need to overlay use, but it is relatively simple to use, Grammar and writing formats are easier to remember, so the usage rate is quite high.

Today in the operation of a string, you need to find out a string of a number, although the use of split, substring, indexof the combination of functions can be achieved, but if the use of regular expressions, it is much better, the most important function of regular expressions is not a match? Then try to use regular expressions to write the program.

    • var s = ' I am using Kaka net www.webkaka.com to test website speed ';
    • var s = s.replace (/([\s\s]*) Kaka net ([A-Z.] *) ([\s\s]*)/ig, ' $ ");
    • alert (s);

Such results can be obtained by the URL "www.webkaka.com".

However, when the "Kaka net" of the above statement is a variable, the replace () problem occurs.

I started writing this:

    • var s = ' I am using Kaka net www.webkaka.com to test website speed ';
    • var words = ' kaka net ';
    • var s = s.replace (/([\s\s]*) ' +words+ ' ([A-Z.] *) ([\s\s]*)/ig, ' $ ");
    • alert (s);

But the result is: I'm using Kaka www.webkaka.com to test the speed of the website. That is, the URL is not taken out.

And then tried a lot of ways, not good.

It seems that the use of replace () is not working, the final solution to become more complex, the following wording:

    • var s = ' I am using Kaka net www.webkaka.com to test website speed ';
    • var words = ' kaka net ';
    • var regexp=new regExp ("([\s\s]*)" +words+ "([A-Z.] *) ([\s\s]*) "," GMI ");
    • Regexp.test (s);
    • var url = regexp.$2;
    • alert (URL);

The operating result is: www.webkaka.com

The RegExp class is used here, which explains the meaning of the above statement.

    • var regexp=new regExp (); is to define a regular expression instance

[\s\s]* can match all characters, including non-visible symbols such as line breaks.]

[A-Z.] * is a match URL, of course, this writing can only match the URL containing lowercase characters, the extension is [a-za-z0-9.~]* can contain most of the URLs.

GMI Yes, G represents a global scan, and if not, then only the first match is matched. Once the G option is added, each match will be matched down. When no matching characters are scanned, NULL is returned. I means case insensitive.

Regexp.test (s); S is the original string, using RegExp to match the desired string.

regexp.$2; This is the 2nd match, and each parenthesis () is a match.

Knowledge expansion

The basic syntax and simple example of JS regular expression

1. JavaScript regular object creation and usage

Declaring a JavaScript regular expression

    • var recat = new RegExp ("cat");

You can also

    • var recat =/cat/; Perl Style (recommended)

2. Learn the most common test exec match search replace split 6 methods

1) test checks whether the specified string exists

    • var data = "123123";
    • var recat =/123/gi;
    • Alert (recat.test (data)); True

Check if the character exists G continue down I case-insensitive

2) Exec return query value

    • var data = "123123,213,12312,312,3,cat,cat,dsfsdfs,";
    • var recat =/cat/i;
    • Alert (recat.exec (data)); Cat

3) match gets query array

    • var data = "123123,213,12312,312,3,cat,cat,dsfsdfs,";
    • var recat =/cat/gi;
    • var arrmactches = Data.match (recat)
    • for (Var i=0;i < arrmactches.length; i++)
    • {
    • Alert (Arrmactches[i]); Cat Cat
    • }

4) search returns location similar to IndexOf

    • var data = "123123,213,12312,312,3,cat,cat,dsfsdfs,";
    • var recat =/cat/gi;
    • Alert (Data.search (Recat)); 23

5) Replace replacement character with regular substitution

    • var data = "123123,213,12312,312,3,cat,cat,dsfsdfs,";
    • var recat =/cat/gi;
    • Alert (Data.replace (Recat, "libinqq"));

6) split using regular split array

    • var data = "123123,213,12312,312,3,cat,cat,dsfsdfs,";
    • var recat =/\,/;
    • var arrdata = Data.split (recat);
    • for (var i = 0; i < arrdata.length; i++)
    • {
    • Alert (Arrdata[i]);
    • }

3, learning the simple class negative class scope class combination class

Simple class

    • var data = "1LIBINQQ,2LIBINQQ,3LIBINQQ,4LIBINQQ";
    • var recat =/[123]libinqq/gi;
    • var arrdata = Data.match (recat);
    • for (var i = 0; i < arrdata.length; i++)
    • {
    • Alert (Arrdata[i]); 1LIBINQQ 2LIBINQQ 3LIBINQQ
    • }

Negative to Class

    • var data = "ALIBINQQ,1LIBINQQ,2LIBINQQ,3LIBINQQ,4LIBINQQ"; \u0062cf
    • var recat =/[^a123]libinqq/gi;
    • var arrdata = Data.match (recat);
    • for (var i = 0; i < arrdata.length; i++)
    • {
    • Alert (Arrdata[i]); 4libinqq
    • }

Scope class

    • var data = "Libinqq1,libinqq2,libinqq3,libinqq4,libinqq5"; \u0062cf
    • var recat =/libinqq[2-3]/gi;
    • var arrdata = Data.match (recat);
    • for (var i = 0; i < arrdata.length; i++)
    • {
    • Alert (Arrdata[i]); LIBINQQ2 libinqq3
    • }

Combination Class

    • var data = "a,b,c,w,1,2,3,5"; \u0062cf
    • var recat =/[a-q1-4\n]/gi;
    • var arrdata = Data.match (recat);
    • for (var i = 0; i < arrdata.length; i++)
    • {
    • Alert (Arrdata[i]); A b C 1 2 3
    • }

You may also be interested in the following articles

ASP. NET regular expression extracts web page URLs, headings, picture instances, and filters all instances of HTML tags

Summary: Several ways to split a string with ASP.

Using regular expressions in Java to determine whether a string is numeric

JS Regular expression Replace has variables in the solution to use the RegExp class

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.