0

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

1
  • 3
    jQuery is not meant to manipulate text/javascript. It's just used to access/hack/whatever the DOM. Commented Oct 11, 2011 at 16:39

4 Answers 4

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Noticing how markdown is confused by "\", I'm curious if JavaScript would understand it, or if it should be my_string.split("\\")[1]?
Yes, you need the two slashes to make it work even in the assignment. In this case even the assignment will ignore the slash.
1

You may use

text.substring(fromindex,toindex)

Comments

0

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

1 Comment

Or maybe just string.split('\')?
0

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]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.