Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用正则表达式从字符串中获取短语_Javascript_Arrays_Regex_Expression_Dom Events - Fatal编程技术网

Javascript 如何使用正则表达式从字符串中获取短语

Javascript 如何使用正则表达式从字符串中获取短语,javascript,arrays,regex,expression,dom-events,Javascript,Arrays,Regex,Expression,Dom Events,我如何才能从字符串中仅获取文本text\u add()for\u delete()此编辑() 并将它们放在一个数组中。 我试图占用一个正则表达式,并根据数组的数据进行过滤,但这对我不起作用。 谁能帮我 let filter=["_add()","_delete()","_edit()"]; var cadena="text_add()-----for_delete()___ this_edit() this example

我如何才能从字符串中仅获取文本
text\u add()
for\u delete()
此编辑()
并将它们放在一个数组中。 我试图占用一个正则表达式,并根据数组的数据进行过滤,但这对我不起作用。 谁能帮我

let filter=["_add()","_delete()","_edit()"];

var cadena="text_add()-----for_delete()___ this_edit()  this example is a test hello world";
    
               var myMethod = new RegExp('(\\w*' + filter + '\\w*)', 'gi');
               var matchesMethod = r.match(myMethod);

               if (matchesMethod != null) {
                   arrayMethod.push(matchesMethod.toString().split(","));
               }
尝试:


此处

如果要匹配这些精确匹配,还可以检查字符串是否包含其值,而不是使用正则表达式。
var cadena="text_add()-----for_delete()___ this_edit()  this example is a test hello world";
var arr = cadena.match(/(\w*\(\))/g);
\w :  Matches any word character (alphanumeric & underscore).
*  :  Match 0 or more characters.
\( :  Matches a "(" character.
\) :  Matches a ")" character.
g  :  global flag