开发者

how to find number pattern in a string

I am in the middle of completing some code, I almost finished my code but I am not posting 开发者_开发问答that code here as that is huge, I am copying a class name from one div to another div and I want to extract a number from that class, the class always follow the below pattern. The class will always have ColorSlice before number and Mouseover after the number.

class names

ColorSlice1MouseOver
ColorSlice1MouseOver
ColorSlice12MouseOver
ColorSlice32MouseOver

I want to extract the number from that string in a elegant fashion using regex or so, I can write substring functions, but I thought there might be better way to do that. Appreciate any help.


How about this regex (assuming you have original css class name in Javascript variable str):

num = str.replace(/ColorSlice([0-9]+)MouseOver/, "$1");


"ColorSlice1MouseOver".replace("ColorSlice", "").replace("MouseOver", "");
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜