Вопрос

I have the following string

/Date(1317772800000)/

I want to use a Javascript regular expression to extract the numerical portion of it

1317772800000

How is this possible?

Это было полезно?

Решение

That should be it

var numPart = "/Date(1317772800000)/".match(/(\d+)/)[1];

Другие советы

No need for regex. Use .substring() function. In this case try:

var whatever = "/Date(1317772800000)/";
whatever = whatever.substring(6,whatever.length-2);

This'll do it for you: http://regex101.com/r/zR0wH4

var re = /\/Date\((\d{13})\)\//;
re.exec('/Date(1317772800000)/');
=> ["/Date(1317772800000)/", "1317772800000"]

If you don't care about matching the date portion of the string and just want extract the digits from any string, you can use this instead:

var re = /(\d+)/;
re.exec('/Date(1317772800000)/')
["1317772800000", "1317772800000"]
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top