2013-04-18 3 views

답변

1

이 같은 경기를 사용할 수 있습니다

var str = "item4", 
    num = +str.match(/item(\d+)/)[1]; // => 4 

나는 숫자로 캐스팅 단항 +을 사용했다. 대신 parseInt 또는 Number 생성자를 사용할 수 있습니다.

0

캡처 그룹과 함께 .match(..)을 사용해야합니다.

"floatLeft item4".match(/item(\d+)/) 
// Result: ["item4", "4"] 
2
var str = "item4", 
    num = (str.match(/item([0-9]+)/)||[])[1]; // if no match, `match` will return null therefore the `|| []` (or empty array). 

console.log(num); // "4" (typeof num === "string") 

console.log(+num) // 4 (typeof num === "number") 
+0

+ 1을 사용하는 경우 – user2153497

2

교체 사용 :

"floatLeft item4".replace(/.*item(\d+)/,"$1")

일치하여 :

(많이 일치 등)

"floatLeft item4".match(/item(\d+)/)[1]

간부를

/item(\d+)/.exec("floatLeft item4")[1]

(많이 일치처럼 다시) 분할을 사용하여 :

"floatLeft item4".split(/item(\d+)/)[1]

http://jsfiddle.net/UQBNn/

split 방법 (IE처럼 ..) 모든 브라우저에서 지원되지 않습니다하지만

+0

<= IE8에서 정규 표현식으로 분할하지 말아야합니다. – andlrc

+0

예, IE8에서 테스트했지만 작동하지 않았습니다. .. 좋은 옛 IE .. – user2153497