Home  >  Article  >  Web Front-end  >  Debugging problems encountered with regular expressions in JavaScript_javascript tips

Debugging problems encountered with regular expressions in JavaScript_javascript tips

WBOY
WBOYOriginal
2016-05-16 16:17:401168browse

Regarding the problem of string testing in regular expressions, we generally have two methods: test() and exec(). I will not elaborate on the difference between these two methods here. The main thing is that test() returns Boolean value, exec() returns the matched character array, but today I encountered a problem. The program is very simple, but the problems inside make me puzzled. I couldn't find the answer when I searched for it on the Internet (personally I am not good at it).

Copy code The code is as follows:

var pattern=new RegExp("match","ig");
alert(pattern.test("Match"));
alert(pattern.exec("Match"));

The value of the first alert() is true as expected

But the second test value is unexpectedly null

I don’t know what the reason is for this problem because I haven’t seen the source code of the function. If anyone can point it out, I’d be grateful! !

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