Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove specific text from variable - jquery

Tags:

javascript

I have a variable in my script containing data test/test1. The part test/ is already stored in another variable. I want to remove test/ from the previous variable and want to store remaining part in another variable. how can I do this??

Thanks in advance...:)

blasteralfred

like image 235
Alfred Avatar asked Apr 05 '11 16:04

Alfred


People also ask

How to remove specific text in jQuery?

jQuery remove() MethodThe remove() method removes the selected elements, including all text and child nodes. This method also removes data and events of the selected elements. Tip: To remove the elements without removing data and events, use the detach() method instead.

How to remove a value in jQuery?

To remove elements and content, there are mainly two jQuery methods: remove() - Removes the selected element (and its child elements) empty() - Removes the child elements from the selected element.

How do you cut a string in JQ?

To trim a string in jQuery, use the trim() method. It removes the spaces from starting and end of the string.

How do I remove something from a string in JavaScript?

The replace() method is one of the most commonly used techniques to remove the character from a string in javascript. The replace() method takes two parameters, the first of which is the character to be replaced and the second of which is the character to replace it with.


2 Answers

In your case, x/y:

var success = myString.split('/')[1]

You split the string by /, giving you ['x', 'y']. Then, you only need to target the second element (zero-indexed of course.)

Edit: For a more general case, "notWantedwanted":

var success = myString.replace(notWantedString, '');

Where notWantedString is equal to what you want to get rid of; in this particular case, "notWanted".

like image 77
Zirak Avatar answered Sep 18 '22 13:09

Zirak


If your requirement is as straightforward as it sounds from your description, then this will do it:

var a = "test/test1";
var result = a.split("/")[1];

If your prefix is always the same (test/) and you want to just strip that, then:

 var result = a.substring(5);

And if your prefix varies but is always terminated with a /, then:

var result = a.substring(a.indexOf("/") + 1);
like image 39
Town Avatar answered Sep 20 '22 13:09

Town