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
Post a Comment