Remove specific text from a variable - jquery

I have a variable in my script containing test/test1 data. The test/ already stored in another variable. I want to remove test/ from a previous variable and want to save the rest in another variable. How can i do this?

Thanks in advance ... :)

blasteralfred

+6
javascript
source share
4 answers

In your case x/y :

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

You split the string into /, giving you ['x', 'y'] . Then you just need to target the second element (of course, a zero index).

Edit: for the more general case of "notWantedwanted":

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

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

+8
source share

To break at the first appearance of "/":

 var oldstring = "test/test1"; var newstring = oldstring.substring(oldstring.indexOf("/")+1); 

There are many other ways to do this, other answers work just fine too.

+1
source share

If your requirement is as simple as it sounds from your description, then this will be done:

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

If your prefix is ​​always the same (test /), and you just want to remove it, then:

  var result = a.substring(5); 

And if your prefix changes, but always ends with a /, then:

 var result = a.substring(a.indexOf("/") + 1); 
+1
source share

Make a choice:

JavaScript function replace () .

 var data = "test/test1"; data = data.replace(/data/gi, 'test/'); 

Or:

 var data = "test/test1"; var dataArray = data.split('/'); var data1 = dataArray[0]; var data2 = dataArray[1]; 
0
source share

All Articles