简体   繁体   English

使用JavaScript / jQuery获取字符串的一部分

[英]Get part of a string using JavaScript/jQuery

我有一个字符串,例如testserver\\sho007 ,如何使用jQuery只返回sho007

You can do it simply with javascript 您可以使用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 编辑以添加Eric提到的双斜杠

You may use 您可以使用

text.substring(fromindex,toindex) text.substring(fromindex,toindex)

如果您确定所需的文本将在斜杠之后,请使用正则表达式。

split the string using "\\" and get the position [1], always start with [0] 使用“ \\”分割字符串并获得位置[1],始终以[0]开头

exp: 经验值:

html: 的HTML:

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

js: js:

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

testserver = [0] 测试服务器= [0]

sho007 = [1] sho007 = [1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM