The parameters in the js regular expression replace are $1, $2, ..., $99, functions, etc. and commonly used regular expressions

grammar:

string.replace(regexp, replacement)

parameter:

regexp: Required. The value or regular expression to search for.

replacement: Required. Specifies replacement text or a function that generates replacement text.

Return Value: A new string with the specified value replaced.

w3school: JavaScript String replace() method

1. Routine use

Replace aaa with 111:

var str = "aaa,bbb,ccc";
str.replace('aaa', "111");//输出'111,bbb,ccc'

Replace consecutive letters with 111:

var str = "aaaaaaa,bbbbbb,ccc";
str.replace(/\w+/g, "111");//输出'111,111,111'

 Replace all letters with 1:

var str = "aaa,bbb,ccc";
str.replace(/\w/g, "1");//输出'111,111,111'

Two, $1, $2, ..., $99

var str = "aaa,bbb,ccc";
str.replace(/(\w+),(\w+),(\w+)/, "$3,$2,$1");//输出'ccc,bbb,aaa'
  • $1 represents the first (\w+) matched content, ie aaa
  • $2 represents the first (\w+) matched content, ie bbb
  • $3 represents the first (\w+) matched content, namely ccc
var str = "aaa、bbb、ccc";
str.replace(/(\w)+、(\w)+、(\w)+/, "$3$2$1");//输出'cba'
  • $1 represents the first (\w) matched content, ie a
  • $2 represents the first (\w) matched content, ie b
  • $3 represents the first (\w) matched content, ie c

Conclusion : $1, $2, ..., $99 represent the result of a bracket match in the regular expression .

3. Function

//输出'hello,world'
var str = "Hello,World";
str.replace(/[A-Z]/g, function(val){
    return val.toLowerCase()
});

//输出'HELLO,WORLD'
var str = "Hello,World";
str.replace(/[a-z]/g, function(val){
    return val.toUpperCase()
});

//输出'000,000,111'
var str = "aaaaaaa,bbbbbb,ccc";
str.replace(/\w+/g, function(val){
    return val === 'ccc' ? '111':'000'
});

A classic example - template character matching: 

var data = {
    title:'标题',
    subTitle: '副标题',
    content:'内容'
}
var str='这是一段内容,标题是{
   
   {title}},副标题是{
   
   {subTitle}},内容是{
   
   {content}}'

str.replace(/\{\{(.*?)\}\}/g, function(val,key){
    return data[key]
});

Output: This is a piece of content, title is title, subtitle is subtitle, content is content

The first parameter of this function is the content of the match, and the second parameter is the result of the parenthesis match

4. Other common regular expressions

Mailbox format:^[a-z0-9A-Z]+[- | a-z0-9A-Z . _]+@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[az]{2,}$

positive integer: ^[1-9]([0-9])*$

ID number ^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12)) (([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$
ip address ^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$
numbers or letters ^([0-9a-zA-Z]+)$
phone number ^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\\d{8}$
Date time format YYYY-MM-DD HH:mm:ss ^(\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2})$
Date format YYYY-MM-DD ^(\\d{4}-\\d{2}-\\d{2})$
legal url

(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]

most three decimals ^(?:[1-9]\\d*|0)(?:\\.\\d{1,3})?$
Greater than or equal to 0 and up to three decimal places ^(?!0+(?:\\.0+)?$)(?:[1-9]\\d*|0)(?:\\.\\d{1,3})?$
up to two decimal places ^(?:[1-9]\\d*|0)(?:\\.\\d{1,2})?$
Greater than or equal to 0 and up to two decimal places ^(?!0+(?:\\.0+)?$)(?:[1-9]\\d*|0)(?:\\.\\d{1,2})?$
At least two characters, Chinese or English ^([a-zA-Z\\u4E00-\\u9FA5]+)$
3-15 characters, only numbers, letters, slashes, dashes, dots ^[-/0-9A-Za-z.]{3,15}$

Guess you like

Origin blog.csdn.net/sxww_zyt/article/details/130258920