Regular expressions in JavaScript

Source: Internet
Author: User
Tags modifier modifiers

Tag: Pat Lin cannot find ASP pattern nod replacement string Reg string

The RegExp object is used in JavaScript to represent regular expressions.

There are 3 methods for RegExp objects, respectively:

1, compile

2. exec

3. Test

So how exactly are these three methods used?

First of all, compile has nothing to do with it, it can only improve a little bit of efficiency when multiple regular matches, not to be considered first.

Here's the exec, which returns the specified value found, the position of the value in the string, and the entire string. If not found, returns NULL.

1 var New REGEXP (' Man ', ' G ');
2 var str1 = "I am a Man"; 3 var str2 = pattern.exec (str1);
4 // [' Man ', Index:7, input: ' I am a man ']

OK, this is very simple, look at the test () method below.

The test method is also very simple, it only returns TRUE or false.

var New REGEXP (' Man ', ' G ');
var str1 = "I am a Man"; var str2 = pattern.test (str1);
Console.log (//true

ES5 before, RegExp all the methods on these three. Normal string also has a direct regular method.

1. Search to find a string

Usage: str.search (' xxxx ')

Return: Found on the return position, can not find return-1.

2. Replace to substitute strings

Usage: str.replace (' string in str ', ' target string ')

Returned: A string was found that returned a successful substitution, and the original string was not found.

3. Match is used to find one or more matching strings.

Usage: ' Me and Me and Me '. Match (/me/g)

return: [' Me ', ' Me ', ' me ']

Of course, match can also be found individually, remove the G on the line

4. Split is used to separate the string into XX

Usage: '. exe,.zip,.tar,.tar.gz '. Split (",")

return: ["EXE", "zip", "tar", "tar.gz"] very useful ^ ^

ES5 has three regexp modifiers, each of which is

1. G: Match the Global qualifying string

2. I: case-insensitive matching

3. M:multiline for multi-line matching

The following is the difference between G I and normal mode, the so-called normal mode, that is, there is no modifier to write a pattern:

1 console.log (' Gourd doll, gourd doll, seven flowers on a Vine '. Match (/gourd/));  // [' Gourd baby ', index:0, input: ' Gourd baby, gourd baby, a vine on the seven Flowers '] 2 console.log (' Gourd doll, gourd doll, seven flowers on a Vine '. Match (/gourd/g));  // [' Gourd baby ', ' Gourd Baby '] 3 4 console.log (' Huluwa, Huluwa, Yi Gen Teng shang qi duo Hua '. Match (/huluwa/g));  // [' Huluwa '] 5 console.log (' Huluwa, Huluwa, Yi Gen Teng shang qi duo Hua '. Match (/huluwa/ig));  // [' Huluwa ', ' Huluwa ']

See the code quite clear. As for the next line, why use the IG Union format? Because just using I will only find the first one to start matching. To match the global, you must use G.

In the ES6 version, new modifiers are added, respectively:

1. U: meaning "Unicode mode", which is used to correctly handle Unicode characters greater than \uffff. That is, the four-byte UTF-16 encoding is handled correctly.

2. Y: Usually called the "sticky" modifier. Functionally basic and G modifiers, the biggest difference is that G is used to match all but each time it starts from scratch and the Y modifier

Matches each match, starting with the remaining string. The babel-node executes the Y modifier with an error and cannot be tested. In the future.

Reference:

Http://www.w3schools.com/jsref/jsref_obj_regexp.asp

http://es6.ruanyifeng.com/#docs/regex

Regular expressions in JavaScript

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.