Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get text from character and after using jQuery

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 in jQuery)

like image 608
Odys Avatar asked Sep 01 '11 10:09

Odys


People also ask

How do I split a string after a specific character?

To cut a string after a specific character, you can use the substring() method, slice() method, or split() method. The slice() and the substring() methods work the same as they extract the string by cutting other parts based on the specific character.

How do you grab substring after a specific character jQuery or JavaScript?

To get the substring after a specific character:Use the split() method to split the string on the character. Access the array of strings at index 1 . The first element in the array is the substring after the character.

How to get a string after a character in JavaScript?

To get a part of a string, string. substring() method is used in javascript. Using this method we can get any part of a string that is before or after a particular character.

How to get part of string in jQuery?

To get substring of a string in jQuery, use the substring() method. It has the following two parameters: from: The from parameter specifies the index where to start the substring. to: The to parameter is optional.


2 Answers

you could do:

var text =  'texttexttext#abc';
var abc = text.substring(text.indexOf('#') +1);
like image 137
Nicola Peluchetti Avatar answered Sep 28 '22 23:09

Nicola Peluchetti


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

like image 38
Richard Dalton Avatar answered Sep 28 '22 22:09

Richard Dalton