如果字符串与javascript中的正则表达式匹配,则从数组中移除字符串
我有一个包含不同字符串的数组。我想删除字符串中与正则表达式匹配的所有非单词如果字符串与javascript中的正则表达式匹配,则从数组中移除字符串,javascript,regex,Javascript,Regex,我有一个包含不同字符串的数组。我想删除字符串中与正则表达式匹配的所有非单词 myarray = ['I want to play football , not watch it yeah:', 'Leeds: play the / worse football'] myregex = /\W+\s/gi 我想删除所有与正则表达式匹配的非单词,但保持字符串不变,包括空格 newarray = ['I want to play football not watch it yeah', 'Lee
myarray = ['I want to play football , not watch it yeah:', 'Leeds: play the / worse football']
myregex = /\W+\s/gi
我想删除所有与正则表达式匹配的非单词,但保持字符串不变,包括空格
newarray = ['I want to play football not watch it yeah', 'Leeds play the worse football']
我不知道如何执行此操作。听起来您想使用字符串来创建数组: 使用此正则表达式:
(\s[^\w ]|[^\w ])
您可以在阵列上使用它,如下所示:
MyArray=[“我想踢足球,而不是看足球耶:”,“利兹:踢更糟糕的足球”]
MyRegex=/(\s[^\w]|[^\w])/gi
NewArray=MyArray.map(函数(字符串){
var NewString=string.replace(MyRegex“”)
document.write(新闻字符串+“
”)
返回新闻字符串
})
您可以迭代数组,应用正则表达式并使用.splice()函数请注意,当前正则表达式与第一个数组末尾的:
不匹配string@Gonzalo.-splice绝对不是从字符串中提取块的正确方法。对不起,我阅读了从数组中删除整个字符串的步骤。那么replace就是答案了。我如何修改正则表达式以不删除包含@符号的单词。例如“@regex”
(\s[^\w ]|[^\w ])
MyRegex = /(\s[^\w ]|[^\w ])/gi;
NewArray = MyArray.map(function(string) {
return string.replace(MyRegex, '')
})