简体   繁体   English

如何从 JavaScript 中的某些子字符串(以列表形式给出)以外的字符串中删除每个字符?

[英]How to remove every character from a string except certain substrings (given as a list) in JavaScript?

In JavaScript, let's assume we have a string: "The quick brown fox jumps over the lazy dog"在 JavaScript 中,假设我们有一个字符串:“The quick brown fox jumps over the lazy dog”

And then we have a list of substrings, say: ["dog", "brown", "The", "jumps"]然后我们有一个子字符串列表,比如:["dog", "brown", "The", "jumps"]

How to filter every other character from the string, but not the substrings given as the list?如何从字符串中过滤每个其他字符,而不是作为列表给出的子字符串?

So the result in this case should be: "Thebrownjumpsdog"所以这种情况下的结果应该是:“Thebrownjumpsdog”

The first solution that came to my mind was to use a loop, and RegExp on every iteration ie:我想到的第一个解决方案是在每次迭代中使用循环和 RegExp,即:

const listOfSubstrings = ["dog", "brown", "The", "jumps"];
let theString = "The quick brown fox jumps over the lazy dog";

for (const substring of listOfSubstrings) {
  theString = theString.replace(new RegExp(`[^${substring}]`, "g"), "");
}

However, the if we take a closer look (or test) the code, we see and understand that there is nothing left after the loop: On every iteration everything but the current element in the list is removed.然而,如果我们仔细查看(或测试)代码,我们会看到并理解循环后什么也没有留下:在每次迭代中,除了列表中的当前元素之外的所有内容都被删除。 To be precise, nothing is left after the second iteration.准确的说,第二次迭代之后什么都没有了。

So, any ideas how the end result, I provided, could be achieved given the string and the list of substrings?那么,在给定字符串和子字符串列表的情况下,我提供的最终结果如何实现的任何想法?

You could match these substrings and join them:您可以匹配这些子字符串并加入它们:

let res = s.match(/(?:dog|brown|The|jumps)/g).join("");

See this JS demo at tio.run or regex demo at regex101 请参阅 tio.run 上的此 JS 演示或 regex101 上的正则表达式演示


To build the pattern from the listOfSubstringslistOfSubstrings构建模式

let regex = new RegExp('(?:' + listOfSubstrings.join("|") + ')','g');

You can try this:你可以试试这个:

const listOfSubstrings = ["dog", "brown", "The", "jumps"];
let theString = "The quick brown fox jumps over the lazy dog";

let result = "";

const theStringArray = theString.split(" ");

theStringArray.forEach(s => {
    if(listOfSubstrings.includes(s)){
        result += s;
    }
})

But this might be slower if your listOfSubstrings is larger.但是,如果您的 listOfSubstrings 较大,这可能会更慢。 For that, you can convert your listOfSubstrings to dictionary为此,您可以将 listOfSubstrings 转换为字典

const listOfSubstrings = ["dog", "brown", "The", "jumps"];
let theString = "The quick brown fox jumps over the lazy dog";

let result = "";

const theStringArray = theString.split(" ");

let substrings = {};

// converting array to dictionary
listOfSubstrings.forEach(e=>{
    substrings[e] = e;
})

theStringArray.forEach(s => {
    if(substrings[s] !== undefined){
        result += s;
    }
})

The reason why using dictionary is that checking if the key exist, works in O(1) but array.includes works in O(n).使用字典的原因是检查键是否存在,在 O(1) 中工作,但 array.includes 在 O(n) 中工作。

This solution splits the input by spaces, filters the list by words of interest, and joins it back to a string without space to get the desired result:此解决方案按空格拆分输入,按感兴趣的单词过滤列表,然后将其连接回没有空格的字符串以获得所需的结果:

 const input = 'The quick brown fox jumps over the lazy dog'; const listOfSubstrings = ["dog", "brown", "The", "jumps"]; let regex = new RegExp('^(' + listOfSubstrings.join('|') + ')$'); let result = input.split(' ').filter(str => regex.test(str)).join(''); console.log(result);

Output: Output:

Thebrownjumpsdog

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM