0

http://www.somewhere.com/0123456789.html

How to extract the 01234567879 from the string?

1
  • Do you already have the string or you want to extract it from current document's URL? Commented Nov 2, 2011 at 9:31

5 Answers 5

3

Some Tutorial might help you getting behind RegExp. Some RegExp Tester might help you testing your regular expressions.

aside from that, you're probably looking for

var string = "http://www.somewhere.com/0123456789.html",
    id = string.match(/\/(\d+)\.html/)[1];
alert(id);
Sign up to request clarification or add additional context in comments.

1 Comment

+1 The only answer that doesn't fail on different domain names, etc.
1

This should do it.

var string = "http://www.somewhere.com/0123456789.html";
string.replace(/^.*\/(.*)\.html$/, "$1");

Comments

1

I would do it like this:

"http://www.somewhere.com/0123456789.html".match(/\/(\d+)\./)[1];

Comments

0
var path = document.location.path;
var id = path.replace('.html', '');

Comments

0

Simply:

var str = "http://www.somewhere.com/0123456789.html";  
var result = /\d+/.exec(str);

Result:

0123456789

1 Comment

Why the down vote? Anyone cares to test the posted code first ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.