Home >Web Front-end >JS Tutorial >Summary of regular knowledge in js

Summary of regular knowledge in js

小云云
小云云Original
2018-03-30 16:07:481061browse

This article mainly shares with you a summary of regular knowledge in js. It mainly explains it to you in the form of code. I hope it can help you.

Method:

1,

test
var string="abvfddsadew";
var reg=/ \D/;
reg.test(string) //记住test是正则表达式对象的方法。返回值为true或false
2. search()用于搜索正则内容,如果搜索到匹配,那么就返回出现的位置,否则返回-1
3. match()用于寻找匹配的字符串片段,返回所匹配的该片段,并且以数组的形式返回。
var string="haj123sdk54hask33dkhalsd879";var reg=/\d+/g;
console.log(string.match(reg));//["123", "54", "33", "879"]


##Change search returns 3

4. replace() replaces the matching string based on match. The passed-in parameter can be a string or a callback function

var string="haj123sdk54hask33dkhalsd879";         
 var reg=/\d+/g          var res=string.replace(reg,"*");        
  console.log(res);//haj*sdk*hask*dkhalsd*        
   var res_2=string.replace(reg,function(string){             
    var add="";              for(var i=0;i!=string.length;i++){                  
    add+="*";             }           
     return add;         })
         console.log(res_2);//haj***sdk**hask**dkhalsd***


##The following introduces the common characters in regular expressions \s : Space
\S : Non-space
\d : Digit
\ D: Non-digits
\w: Characters (letters, numbers, underscore_)
\W: Non-characters
.( dot) - any character
\.: The real dot
\a represents a repeated subitem, such as
/ (a) (b) (c) \1/-----Match abca repeats the first subterm: a
/ (a) (b) (c) \2/-- ----Match abcb repeat the second sub-item: b
\b: independent part (start, end, space)
\B: non-independent Part
[]: represents any one in a certain set, such as [abc]. The whole represents a character matching any one of a b c, or it can be a range. [0-9] The range must be small until big.
[^a] The whole represents one character: ^ written inside [] means exclusion

():, Parentheses are called grouping symbols. It is equivalent to the brackets in mathematics

Let me emphasize here, except for the two regular expression methods test and exec (reg.test(string) or reg.exec(string) ), the others are all string methods

.

match is a string method, written as: str.match(reg)


exec is a regular expression Method, written as: reg.exec(str)

exec returns an array of matching items. When match does not add g, both is a consistent result.

Add g to match to return multiple matching arrays.

Related recommendations:

Summary of some commonly used regular expressions

How to verify non-zero negative integers in JS regular expressions

How to check non-zero positive integers using JS regular expressions

The above is the detailed content of Summary of regular knowledge in js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn