简体   繁体   English

如何从javascript中的字符串数组中删除动态文本

[英]How can I remove dynamic text from an array of strings in javascript

I have a string with a lot of random words: example var stringWithWords = "hello my name is" the separator between the words are a space.我有一个包含很多随机单词的字符串:example var stringWithWords = "hello my name is"单词之间的分隔符是一个空格。

I have also a variable var word1 = "name".我还有一个变量 var word1 = "name"。 (BUT its dynamic, it can change and I dont know exactly where it is in the string above). (但它是动态的,它可以改变,我不知道它在上面的字符串中的确切位置)。

Now the question: How can I remove word1 from the string stringWithWords ?现在的问题是:如何从字符串 stringWithWords 中删除 word1 ?

Try this:试试这个:

array = array.map(x => {
    return x.replace(name, '').trim();
})

That's for an array, i think you changed your question since I first read it tho...那是一个数组,我想自从我第一次阅读它以来你改变了你的问题......

For a string just use replace and then trim to remove unnecessary whitespace...对于字符串,只需使用替换然后修剪以删除不必要的空格...

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

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