如果字符串与javascript中的regex匹配,请从数组中删除它们
Remove strings from an array if they match regex in javascript
我有一个包含不同字符串的数组。我想删除字符串中与正则表达式匹配的所有非单词。
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']
我不知道该怎么做。
听起来像是要使用字符串replace
:来map
数组
myregex = /'W+'s/gi;
newarray = myarray.map(function (str) {
return str.replace(myregex, '');
});
使用此RegEx:
('s[^'w ]|[^'w ])
RegExr上的实时演示
您可以在使用.replace()
的阵列上使用它,如下所示:
MyRegex = /('s[^'w ]|[^'w ])/gi;
NewArray = MyArray.map(function(string) {
return string.replace(MyRegex, '')
})
MyArray = ['I want to play football , not watch it yeah:', 'Leeds: play the / worse football']
MyRegex = /('s[^'w ]|[^'w ])/gi
NewArray = MyArray.map(function(string) {
var NewString = string.replace(MyRegex, '')
document.write(NewString + '<br>')
return NewString
})
相关文章:
- 从组件状态的数组中删除元素
- Mongoose-在更新中删除数组元素
- 从数组中删除元素的最佳方法是:javascript/jquery
- 添加和删除隐藏字段数组中的值,而不提交表单
- 从数组中删除重复条目,并在javascript中按顺序排列
- 一些元素没有从数组中删除
- 删除JS数组中的最小数字
- 从数组中删除三元组项
- 删除重复的数组值,然后存储它们[反应]
- 在特定索引处剥离/删除数组中的值
- 数组删除重复结果Javascript
- 字符串替换或通过数组删除
- js中的对象数组:删除重复项
- KnockoutJS.Mapping.FromJS-可观察数组-删除不起作用
- 用于垃圾收集的Javascript数组删除
- 为什么”——数组.删除数组的最后一个元素
- PHP循环通过脚本数组&删除项目
- 谷歌地图API:试图使用数组删除多个标记
- 带有for循环+条件的Javascript数组删除不必要的元素
- 在JavaScript中管理像ArrayList这样的数组:删除一个元素并关闭创建的间隙