javascript 从字符和使用 jQuery 后获取文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7269150/
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-10-25 23:32:01  来源:igfitidea点击:

Get text from character and after using jQuery

javascriptjquerystringsubstring

提问by Odys

I want to get the text from a string after the occurrence of a specific character.

我想在出现特定字符后从字符串中获取文本。

Lets say: texttexttext#abc And I want to get abc

让我们说: texttexttext#abc 我想得到 abc

How is this done in jquery? (This might be trivial to somebody, but I have little exp in jQuery)

这是如何在 jquery 中完成的?(这对某些人来说可能是微不足道的,但我在 jQuery 中几乎没有经验)

回答by Nicola Peluchetti

you could do:

你可以这样做:

var text =  'texttexttext#abc';
var abc = text.substring(text.indexOf('#') +1);

回答by Richard Dalton

You don't need to use jQuery for this. Simple javascript is fine.

您不需要为此使用 jQuery。简单的 javascript 很好。

In this case:

在这种情况下:

var text = 'texttexttext#abc';
var textAfterHash = text.split('#')[1];

or

或者

var textAfterHash = text.substring(text.indexOf('#') + 1);

JSFiddle Example of both

两者的 JSFiddle 示例

回答by cillierscharl

Contrary to popular belief, jQuery isn't needed in every situation ;)

与流行的看法相反,并非在所有情况下都需要 jQuery ;)

Example:

例子:

var x = 'texttexttext#abc';
var y = x.substring(x.indexOf('#') + 1);
alert(y); //abc