在javascript字符串中替换子字符串

时间:2014-05-02 06:09:42

标签: javascript jquery string replace

我在Javascript中有一个字符串,

'WorkExperience_0_companydetails_0_name'

我想获得以下字符串

'WorkExperience_1_companydetails_1_name'

我怎样才能在jQuery中实现这个目标?

3 个答案:

答案 0 :(得分:2)

尝试,

"WorkExperience_0_companydetails_0_name".split("0").join('1');

或者

"WorkExperience_0_companydetails_0_name".replace("0","1");

DEMO

答案 1 :(得分:1)

,因为您希望将其值更新1

var str="WorkExperience_0_companydetails_0_name";

var ar = str.split("_");

var n=parseInt(ar[1],10)+1;
var newStr = str.replace(ar[1],n);

答案 2 :(得分:0)

<ul>
<div class="test"></div>
<li id="foo1">foo</li>
<li id="bar1" class="test">bar</li>
<li id="baz1">baz</li>
<div class="test"></div>
</ul>
<div id="last"></div>



var foo = $( "li" );
// This implicitly calls .first()
console.log( "Index: " + foo.index( "li" ) ); // 0
console.log( "Index: " + foo.first().index( "li" ) ); // 0
var baz = $( "#baz1" );
console.log( "Index: " + baz.index( "li" )); // 2
var listItem = $( "#bar1" );
console.log( "Index: " + listItem.index( ".test" ) ); // 1
var div = $( "#last" );
console.log( "Index: " + div.index( "div" ) ); // 2`enter code here`