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)
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With