以有效的方式从字符串中删除特定单词

发布于 2025-01-19 08:08:23 字数 647 浏览 3 评论 0原文

我试图从一系列字符串中删除几个匹配的单词。 字符串的数组

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谜兔 2025-01-26 08:08:23

创建一个要删除的字符串的数组或集合,然后根据要迭代的单词是否在集合中进行过滤。

const input = ["select from table order by asc limit 10 no binding"]
const wordsToExclude = new Set(['limit', 'order', 'by', 'asc', '10']);
const words = input[0].split(' ').filter(word => !wordsToExclude.has(word));
console.log(words);

如果您实际上不想删除所有这些单词,而只想删除这样一个序列,请使用正则表达式来匹配limit,直到遇到数字。

const input = ["select from table order by asc limit 10 no binding"];
const result = input[0].replace(/order\b.*?\d+ /, '');
console.log(result);

如果序列之间可以出现其他数字,请在末尾匹配 limit \d+,而不仅仅是 \d+

const input = ["select from table order by 1 asc limit 33 no binding"];
const result = input[0].replace(/order\b.*?limit \d+ /, '');
console.log(result);

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.

const input = ["select from table order by asc limit 10 no binding"]
const wordsToExclude = new Set(['limit', 'order', 'by', 'asc', '10']);
const words = input[0].split(' ').filter(word => !wordsToExclude.has(word));
console.log(words);

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.

const input = ["select from table order by asc limit 10 no binding"];
const result = input[0].replace(/order\b.*?\d+ /, '');
console.log(result);

If additional numbers can come in between the sequence, match limit \d+ at the end, rather than just \d+

const input = ["select from table order by 1 asc limit 33 no binding"];
const result = input[0].replace(/order\b.*?limit \d+ /, '');
console.log(result);

可是我不能没有你 2025-01-26 08:08:23

使用过滤器包含

const [string] = ["select from table order by asc limit 10 no binding"];
const exclude = ["limit", "order", "by", "asc", "10"];

const res = string.split(" ").filter(wd => !exclude.includes(wd));

console.log(res)

或者使用 replaceAll

const [string] = ["select from table order by asc limit 10 no binding"];
const exclude = ['limit', 'order', 'by', 'asc', '10'];

const res = exclude.reduce((str, word) => 
  str.replaceAll(word, ''), string).replaceAll(/\s+/g, ' ');
  
console.log(res)

Using filter and includes

const [string] = ["select from table order by asc limit 10 no binding"];
const exclude = ["limit", "order", "by", "asc", "10"];

const res = string.split(" ").filter(wd => !exclude.includes(wd));

console.log(res)

Alternatively using replaceAll

const [string] = ["select from table order by asc limit 10 no binding"];
const exclude = ['limit', 'order', 'by', 'asc', '10'];

const res = exclude.reduce((str, word) => 
  str.replaceAll(word, ''), string).replaceAll(/\s+/g, ' ');
  
console.log(res)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文