开发者

Get text from character and after using jQuery

开发者 https://www.devze.com 2023-04-01 15:20 出处:网络
I want to get the text from a string after the occurrence of a specific character. Lets say: texttexttext#abc

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

How is this done in jquery? (This might be trivial to somebody, but I have little exp 开发者_C百科in jQuery)


you could do:

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


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

In this case:

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

or

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

JSFiddle Example of both


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

Example:

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

精彩评论

暂无评论...
验证码 换一张
取 消