以有效的方式从字符串中删除特定单词
我试图从一系列字符串中删除几个匹配的单词。 字符串的数组
let string = ["select from table order by asc limit 10 no binding"]
以下是我试图摆脱有订单和限制及其价值的所有 ,并保留其余的。我已经尝试了以下内容,但它们都不优雅/高效。
let splitString = string.split(' ');
let str1 = 'limit';
let str2 = 'order';
let str3 = 'by';
let str4 = 'asc';
let str5 = '10';
splitString = splitString.filter(x => x !== str1);
splitString = splitString.filter(x => x !== str2);
splitString = splitString.filter(x => x !== str3);
splitString = splitString.filter(x => x !== str4);
splitString = splitString.filter(x => x !== str5);
是否有适当的方法可以从字符串中摆脱这些单词? tia
I am trying to strip out a couple of matching words from an array of string. Below is the array containing the string
let string = ["select from table order by asc limit 10 no binding"]
I am trying to get rid of any that has order by and limit and its value and preserving the remaining. I have tried the following but none of them are elegant/efficient.
let splitString = string.split(' ');
let str1 = 'limit';
let str2 = 'order';
let str3 = 'by';
let str4 = 'asc';
let str5 = '10';
splitString = splitString.filter(x => x !== str1);
splitString = splitString.filter(x => x !== str2);
splitString = splitString.filter(x => x !== str3);
splitString = splitString.filter(x => x !== str4);
splitString = splitString.filter(x => x !== str5);
Is there a proper way of getting rid of those words from the string? TIA
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建一个要删除的字符串的数组或集合,然后根据要迭代的单词是否在集合中进行过滤。
如果您实际上不想删除所有这些单词,而只想删除这样一个序列,请使用正则表达式来匹配
limit
,直到遇到数字。如果序列之间可以出现其他数字,请在末尾匹配
limit \d+
,而不仅仅是\d+
Make an array or Set of the strings you want to remove, then filter by whether the word being iterated over is in the Set.
If you don't actually want to remove all of those words, but only such a sequence, use a regular expression to match
limit
until you come across numbers.If additional numbers can come in between the sequence, match
limit \d+
at the end, rather than just\d+
使用
过滤器
和包含
或者使用
replaceAll
Using
filter
andincludes
Alternatively using
replaceAll