Javascript 在预定义字符串后删除字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13634633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 12:32:46  来源:igfitidea点击:

Remove string after predefined string

javascriptreplace

提问by JCHASE11

I am pulling content from an RSS feed, before using jquery to format and edit the rss feed (string) that is returned. I am using replaceto replace strings and characters like so:

在使用 jquery 格式化和编辑返回的 rss 提要(字符串)之前,我正在从 RSS 提要中提取内容。我replace用来替换字符串和字符,如下所示:

var spanish = $("#wod a").text();
var newspan = spanish.replace("=","-");
$("#wod a").text(newspan);

This works great. I am also trying to remove all text after a certain point. Similar to truncation, I would like to hide all text starting from the word "Example".

这很好用。我还试图在某个时间点后删除所有文本。与截断类似,我想隐藏从“示例”一词开始的所有文本。

In this particular RSS feed, the word example is in every feed. I would like to hide "example"and all text the follows that word. How can I accomplish this?

在这个特定的 RSS 提要中,示例这个词出现在每个提要中。我想隐藏"example"并且所有文本都跟在那个词之后。我怎样才能做到这一点?

回答by VisioN

Though there is not enough jQuery, you even don't need it to remove everything after a certain word in the given string. The first approach is to use substring:

尽管没有足够的 jQuery,您甚至不需要它来删除给定字符串中某个单词之后的所有内容。第一种方法是使用substring

var new_str = str.substring(0, str.indexOf("Example"));

The second is a trick with split:

第二个是一个技巧split

var new_str = str.split("Example")[0];

回答by nnnnnn

jQuery isn't intended for string manipulation, you should use Vanilla JSfor that:

jQuery 不用于字符串操作,您应该使用Vanilla JS

newspan = newspan.replace(/example.*$/i, "");

The .replace()methodaccepts a regular expression, so in this case I've used /example.*$/iwhich does a case-insensitive match against the word "example" followed by zero or more of any other characters to the end of the string and replaces them with an empty string.

.replace()方法接受正则表达式,因此在这种情况下,我使用了/example.*$/i它对单词“example”进行不区分大小写的匹配,后跟零个或多个任何其他字符到字符串的末尾,并用空字符串替换它们.

回答by patad

If you also want to keep "Example" and just remove everything afterthat particular word, you can do:

如果您还想保留“示例”并删除该特定单词之后的所有内容,您可以执行以下操作:

var str = "aaaa1111?bbb&222:Example=123456",
    newStr = str.substring(0, str.indexOf('Example') + 'Example'.length);

// will output: aaaa1111?bbb&222:Example

回答by masukomi

I would like to hide all text starting from the word "Example"

我想隐藏从“示例”这个词开始的所有文本

A solution that uses the simpler replace WITH backreferences so as to "hide" everything starting with the word Example but keeping the stuff before it.

一种使用更简单的替换 WITH 反向引用的解决方案,以便“隐藏”以单词 Example 开头的所有内容,但保留它之前的内容。

var str = "my house example is bad"
str.replace(/(.*?) example.*/i, "")     // returns "my house"
// case insensitive. also note the space before example because you 
// probably want to throw that out.