开发者

Get part of a string using JavaScript/jQuery

开发者 https://www.devze.com 2023-04-12 19:46 出处:网络
I have a string, for example testserver\\sho007, how do I use jQue开发者_JAVA百科ry to return only sho007?You can do it simply with javascript

I have a string, for example testserver\sho007, how do I use jQue开发者_JAVA百科ry to return only sho007?


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


You may use

text.substring(fromindex,toindex)


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


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]

0

精彩评论

暂无评论...
验证码 换一张
取 消