user989865
user989865

Reputation: 637

Get part of a string using JavaScript/jQuery

I have a string, for example testserver\sho007, how do I use jQuery to return only sho007?

Upvotes: 0

Views: 1397

Answers (4)

Johnny Craig
Johnny Craig

Reputation: 5000

You can do it simply with javascript

 var my_string="testserver\sho007";
var left_side=my_string.split("\\")[0];
var right_side=my_string.split("\\")[1];

edited to add the double slash as Eric mentioned

Upvotes: 2

Ricardo Binns
Ricardo Binns

Reputation: 3246

split the string using "\" and get the position [1], always start with [0]

exp:

html:

<a class='test'>testserver\sho007</a>

js:

   $(document).ready(function(){
       var a = $(".test").text();
       var aResult = a.split("\\")[1];
   });

testserver = [0]

sho007 = [1]

Upvotes: 0

Dhiraj
Dhiraj

Reputation: 33628

You may use

text.substring(fromindex,toindex)

Upvotes: 1

Adhip Gupta
Adhip Gupta

Reputation: 7163

Use Regular Expressions if you are sure that the text you want is going to be after the slash.

Upvotes: 0

Related Questions