JavaScript-Can you refer to the regular itself variables in the JS regex?

Source: Internet
Author: User
Tags true true
Like in PHP.

preg_prelace("/<([a-z]+)>(.+?)<\/\\1>/is", $text);

This can be used in my \\1 reference to the first parameter, in the JS regular expression is it possible to use this?

Reply content:

Like in PHP.

preg_prelace("/<([a-z]+)>(.+?)<\/\\1>/is", $text);

This can be used in my \\1 reference to the first parameter, in the JS regular expression is it possible to use this?

\nYou can use it, for example, for \1 \2 example:

var regexp = /(['"])[^'"]*\1/;console.log(  regexp.test("'1'"),  regexp.test('"2"'),  regexp.test('"3\'')); /* true true false */

Of course you can.

/<([a-z]+)>(.+?)<\/\1>/.exec("xxx")

PS. Because it's not in a string, so you don't need two backslashes.

Yes, but JS String.replace(reg,newStr) can directly use the regular literal, so do not wrap in the string inside.
1 2 n The number of substrings in the regular; you can use $ $n to refer to the string content that is matched to
For example, replace the matching tag with an empty string:

var str = '

hello world

';str.replace(/<([a-z]+)>(.+)<\/\1>/i,'<$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.