Preface: The previous article on the front-end development of regular expressions about some regular expression basic knowledge, this article is mainly about the re – development of regular expressions often used;
Here is a supplement to the basic knowledge forgotten in the previous article. Replacement in regular expressions is often used in development to filter out some sensitive words and replace them with the data they want.
Substitutions in regular expressions
The replace replacement:
The replace() method replaces a string, either as a string or as a regular expression. stringObject.replace(regexp/substr,replacement)
- First argument: the string or regular expression to be replaced
- The second argument: the new string replaced by
- Return value: a new string that has been replaced
var str="Lili and Andy"
// Text replacement
console.log(str.replace("lili"."HH")) / / HH and Andy
console.log(str.replace(/lili/."AA")) / / AA and Andy
// Function substitution
Copy the code
Arguments to regular expressions:
Switches (also known as modifiers) are matched in three different patterns:
- G: Global matching
- I: Ignore case
- Gi: Global matching and case ignoring
var str2="Lili and Qianqian go to Lili's house,TT"
console.log(str2.replace(/lili|TT/gi."CC")) //CC and Qianqian go to CC's house,CC
Copy the code
Regular expressions commonly used in development:
- English, numeric, case insensitive: ^[A-zA-z0-9]*$
- No special characters or Spaces are supported: ^[\u4E00-\ u9FA5A-zA-z0-9]+$
- Chinese, Numerals, English case and underline: /^[\ u4E00-\ U9FA50-9A-zA-z_]+$/