在字符串对象中找到匹配模式 [英] find match pattern in string object
本文介绍了在字符串对象中找到匹配模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的字符串带有值,我想识别模式 在像这样的字符串中
I have string with values and I want to identify the pattern in the string like
value:function()
like name:function()
or
click:function()
我想忽略空格,并且值不能以下划线开头,而且
I want to ignore space and value cannot start with underscore and in addition
我有一些要忽略的单词数组,例如用户,地址
I have some array with word that I want to ignore like user ,address
即,如果我具有以下
name:function()
click:function()
_press:function()
user: function()
最后的数组只有[name,click]
我尝试按照Avinash的建议执行以下操作,但是它不适用于所有情况, 有什么主意吗?
I try like following as suggested by Avinash but its not working for all the conditions, any idea?
str.match(/\w+(?=\s*:\s*function)/g)
推荐答案
您似乎想要这样的东西,
It seems like you want something like this,
\b(?!address|user|_)\w+(?=\s*:\s*function)
> s = 'name:function()\n\nclick:function()\n\n_press : function()\n\nuser: function'
> s.match(/\b(?!address|user|_)\w+(?=\s*:\s*function)/g)
[ 'name', 'click' ]
这篇关于在字符串对象中找到匹配模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文