开发者

Splitting ID of element to return only the number

开发者 https://www.devze.com 2023-02-12 04:02 出处:网络
I have this line of code: <a href=\"#\" id=\"inv_1\" class=\"load_invoice\"> I am trying to use Javascript to spit back only the integer to be used for the rest of my control. What is the bes

I have this line of code:

<a href="#" id="inv_1" class="load_invoice">

I am trying to use Javascript to spit back only the integer to be used for the rest of my control. What is the best method to do so?

Would .split be used or a simple regex开发者_如何学Go?


I usually use 'replace' for such things:

var myvar = 'inv_1';
myvar = myvar.replace('inv_','');
// myvar is now 1

http://www.w3schools.com/jsref/jsref_replace.asp

You can also use 'split':

var myvar = 'inv_1';
myvar = myvar.split('_');
// myvar[1] is now 1

http://www.w3schools.com/jsref/jsref_split.asp

0

精彩评论

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