简体   繁体   English

如何在javascript中删除一个单词后的所有字符串?

[英]how to remove all the string after a word in javascript?

I want to remove all the string after a word in javascript like in my case remove everything after "share something " 我想删除javascript中一个单词后的所有字符串,例如在我的情况下删除“共享内容”后的所有字符串

bla 
bla bla 

share something ...

bla bla bla 
<image tag></img>
bla bla bla 
bla bla bla 
<image tag></img>

It can be done the following way : 可以通过以下方式完成:

var input='bla\n\nbla\n\nShare something ...\n\nbla bla bla\n<image tag></img>bla\n<image tag></img>';

var output = input.split('Share something')[0]+'Share something';

console.log(output);

You want to tokenize and you can use split for that: 您要标记化,可以为此使用split

Split a string into an array of substrings: 将字符串拆分为子字符串数组:

var str = "How are you doing today?";
var res = str.split(" ");

And to get the first token: res[0] is equal to "How" 并获得第一个令牌: res [0]等于“ How”

In the above example, the delimiter is a space character. 在上面的示例中,定界符是空格字符。 For your specific case, you could make the delimiter "share something " : 对于您的特定情况,可以使定界符“共享某些内容”

var str = "bla\n\nbla\n\nshare something ...\n\nbla bla bla\n<image tag></img>bla\n<image tag></img>"
var res = str.split("share something ");

Using slice you can get a piece of the array: 使用slice可以得到一部分数组:

var whatyouwant = res.slice(0,0) + "share something";

Or 要么

var whatyouwant = res[0] + "share something";

Another option using String.prototype.indexOf and String.prototype.substring : 使用String.prototype.indexOfString.prototype.substring另一个选项:

var seekString = "Share something...";

var str = "Lots of text<br>Share something...<br>More text and stuff";
var idx = str.indexOf(seekString);

console.log("The index is:"+idx);
if (idx !== -1) {
  var result = str.substring(0, idx + seekString.length);
  console.log(result);
}

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

相关问题 如何使用javascript删除字符串中指定单词之后和句点之前的所有字符? - How can I remove all characters after a specified word and before a period in a string using javascript? 如何使用 JavaScript 删除字符串中的最后一个单词 - How to remove the last word in a string using JavaScript Javascript 和正则表达式:删除字符串中最后一个单词后的空格 - Javascript and regex: remove space after the last word in a string 如何使用JavaScript正则表达式删除所有&#39;#&#39;符号(如果它们在单词开头) - How to remove all '#' symbols if they are at the start of word using javascript regular expression 如何删除Javascript字符串中所有出现的字符&#39;? - How to remove all occurrence of character ' in a string in Javascript? 如何删除JavaScript中所有字符串后跟(-)连字符 - How to remove all string followed by (-) hyphen in javascript 如何删除(Javascript)字符串中除表情符号外的所有表情符号? - How to remove all but emojis in a (Javascript) string? 如何删除 Javascript 中所有出现的字符串 - How to remove all occurrences of string in Javascript 如何使用JavaScript从字符串中删除第一个单词“ the” - How to remove first word “the” from string using javascript 如何使用JavaScript从div内容中删除单词/字符串 - How to remove a word/string from div content using javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM