Summary of JS Regular expressions

Source: Internet
Author: User
Tags character classes

When you need to pass a parameter, you must use the new RegExp ();

var re = new RegExp (' \\b ' +sclass+ ' \\b ');

-------------------------------------------------------------
Quantifier, escape character:

\s: Space
\s: Non-whitespace

\d: Digital
\d: Non-digital

\w: Characters (Letters, numbers, underscores)
\w: Non-character

\. : The real point

\b: Separate section (Start, end, space)
\b: A non-independent part

^: Put in front of the regular, represent the beginning
$: End of Representative


/^[1-9]\d{4,11}$///QQ Detection


+: appears at least once;
| : OR
^: non-[in brackets inside the meaning of right and wrong]
. : Any character


{}:
{4,7}: Minimum 4 times, maximum 7 times
{4,}: At least 4 times, no Limit
{4}: exactly 4 times

+:{1,} that appears at least 1 times
?: {0,1} occurs 0 or 1 times
*:{0,} that appears at least 0 times


-------------------------------------------------------------
Identity: Case insensitive I global match g

The regular default is case-sensitive,
If case insensitive, Mark I at the end of the regular;
The regular match succeeds and the match is not continued.
If you want to find all, Mark G;(Global match at the end of the regular)

-------------------------------------------------------------
Four common methods of test search match replace

1, regular. Test (string) [Match succeeded: return True, failed: return FALSE]
2, String. Search (regular) [match succeeded: return position, Failure: return-1]
3, String. Match (regular) [Match succeeded: Returns the matching successful array, failed: return NULL]
4, String. Replace (regular, second argument)
Replace the second argument can be a [string],
It can also be a [callback function], the first parameter of the function is the character that matches the success,
String. replace (regular, function ($0,$1,$2) {})

-------------------------------------------------------------

Match subkey: Parentheses ()
[Group Operations]

String. replace (regular, function ($0,$1,$2) {})
$: Mother; (overall)
$: first child; (first parenthesis)
$: Second child; (second parenthesis)

var str = ' abc ';
var re=/(a) (b) (c)/;
Str.match (re); Back to [Abc,a,b,c]
Match only has no G (global match) to get children

-------------------------------------------------------------
Character classes: A set of similar elements
[] The whole of the brackets represents one character

var str= ' abc ';

var re=/a[bde]c/;

Alert (Re.test (str)); Returns True

var str= ' ABDC ';

var re=/a[bde]c/;

Alert (Re.test (str)); Returns false

var str= ' abc ';

var re=/a[a-z0-9a-z]c/;

Alert (Re.test (str));

-------------------------------------------------------------

\b: Separate section (Start, end, space)

var str= ' Onetwo ';

var re=/\bone/; True

var re=/one\b/; False

Alert (Re.test (str));

-------------------------------------------------------------
Repeating children


\1: Duplicate First subkey
\2: Duplicate Second subkey

var str = ' ABCA ';
var re =/(a) (b) (c) \1/; True
var re =/(a) (b) (c) \2/; False

Alert (Re.test (str));


var re =/\w\w/; C9
var re =/(\w) \1/; CC 99

-------------------------------------------------------------
Find the most frequently occurring characters and times

var str= ' Ahlihkhkjsssssssssssssdasdasdasdasdsdafafqrghjff ';

var arr = Str.split ("); Split array

Str=arr.sort (). Join (');//Array string

var re=/(\w) \1+/g;
var index=0;
var value= ';

Str.replace (Re,function ($0,$1) {
if (index<$0.length) {
Index=$0.length;
value=$1;
}
})

-------------------------------------------------------------

Match Chinese: [\u4e00-\u9fa5]
Line trailing space: ^\s*|\s*$
Email:^\[email protected][a-z0-9]+ (\.[ a-z]+) {1,3}$
URL: [a-za-z]+://[^\s]*
QQ No.: ^[1-9][0-9]{4,9}$
Postcode: [1-9]\d{5}
ID: [1-9]\d{14}| [1-9]\d{17}| [1-9]\d{16}x

Summary of JS Regular expressions

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.