-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.

Sourabh Banka
  • 1,080
  • 3
  • 24
  • 48

4 Answers4

1

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

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

another way to do it

let str = "url(#123456)";

console.log(str.match(/\d+/)[0])
Abslen Char
  • 3,071
  • 3
  • 15
  • 29
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);
CodeF0x
  • 2,624
  • 6
  • 17
  • 28
0

Using only Array methods:

console.log("url(#123456)".split('#').pop().slice(0, this.length -1))
connexo
  • 53,704
  • 14
  • 91
  • 128