开发者

Javascript regex to match 'int'

开发者 https://www.devze.com 2023-03-06 20:26 出处:网络
I nee开发者_Go百科d a regex to match INT or INT, or INT , to splita String, the String could also contain similar words like INTEGER, INT444, INTEGER2, etc. but they should be ignored.

I nee开发者_Go百科d a regex to match INT or INT, or INT , to split a String, the String could also contain similar words like INTEGER, INT444, INTEGER2, etc. but they should be ignored.

var str = 'INTEGER INT, INTEGER2 INT , TEST INT';
str = str.split(/INT[\,\)\s]*[^A-Za-z0-0]/gi);

The regex only matches INT, (INT + comma) and INT , (INT + space + comma) but not INT (only the word INT).

Any help?


You mean this?

var str = 'INTEGER INT, INTEGER2 INT , TEST INT';
str = str.split(/INT(?=(?: ?,|$))/gi); // ["INTEGER ", ", INTEGER2 ", " , TEST ", ""]

That's called forward lookahead, more about it here: http://www.javascriptkit.com/javatutors/redev2.shtml


I am not sure I understand your question, but I think this is what you were asking for.

INT,|INT |INT(?![A-Za-z0-9])


How about :

/INT\b\s*,?/

on your example, it gives :

["INTEGER ", " INTEGER2 ", " TEST "]


Probably you are looking for word boundaries \b

\bINT\b

\b will match the boundary between a character included in \w and other characters.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号