What is the difference between Regex and string in javascript? -


> "1fff=*; style=mobile".match("[\s]*") [ '', index: 0, input: '1fff=*; style=mobile' ] > "1fff=*; style=mobile".match("[^;]*") [ '1fff=*', index: 0, input: '1fff=*; style=mobile' ] > "1fff=*; style=mobile".match('(^|;)[\s]*style=([^;]*)') null > "1fff=*; style=mobile".match(/(^|;)[\s]*style=([^;]*)/) [ '; style=mobile',   ';',   'mobile',   index: 6,   input: '1fff=*; style=mobile' ] 

str.match(str) can work partially regex mode, there difference.

what difference?

the difference in string literal, \s means s — there's no \s escape-sequence string literals, \ gets dropped.

if want use string literal, , need regex contain \s, string literal needs contain \\s (with backslash) string contain \s:

> "1fff=*; style=mobile".match('(^|;)[\\s]*style=([^;]*)') ; style=mobile,;,mobile 

(i recommend sticking regex literal, though.)


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -