-2

I have string in the bellow format:

let str = "url(#123456)";

My string have only number contain in it. It can be any where. I want to extract the number 123456 from the above string.

I am using es6.

1
  • 1
    Use a regex. Use regex101.com Commented Mar 21, 2018 at 12:46

4 Answers 4

1

str.replace(/[^0-9]/g, '')

let str = "url(#123456)";
console.log(str.replace(/[^0-9]/g, ''))

Sign up to request clarification or add additional context in comments.

1 Comment

better to mark as duplicate, right?
0

another way to do it

let str = "url(#123456)";

console.log(str.match(/\d+/)[0])

Comments

0

I did it in a way too complicated way, but it does not involve regex so it's probably better to understand:

let str = "url(#123456)";
str = str.split("#");
str = str[1].split(")");
str = str[0];
console.log(str);

Comments

0

Using only Array methods:

console.log("url(#123456)".split('#').pop().slice(0, this.length -1))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.