Sobiaholic
Sobiaholic

Reputation: 2957

cannot find the match() from <div> in JavaScript

I have question in JavaScript, I'm quite new so be easy on me please :) .

Let's say I have this HTML code:

<body>
<div id="myDivId"> <h3> <p style="text-align: center;"><a href="http://google.com"><strong>TEXT Number 453</strong></a></p></h3></div>
</body>

and when I write to extract the digits only:

var x = document.getElementById("myDivId").innerHTML;

var patt1=/\d{3}/gi;

document.write(x.match(patt1));

The Output will be

NaN not the number 453, but when I remove all the attributes like (hyperlink syntax,

syntax, etc) it does work.

I know I'm doing a mistake, so I hope you enlighten me :).

Upvotes: 0

Views: 95

Answers (3)

Sobiaholic
Sobiaholic

Reputation: 2957

I solved the issue. I was using getElementById() and inside my DIV I have a hyperlink <a> in which the link contained digits, that's why regexp was pulling the NaN error. To solve it I used/learned getElementByTag() and tagged my sentence to avoid the hyperlink code. That fixed the problem :) Thanks, guys!

Upvotes: 2

Selvakumar Ponnusamy
Selvakumar Ponnusamy

Reputation: 5553

Try this script,

var x = document.getElementById("myDivId").innerHTML;
var patt1=/\d{3}/gi;
document.write(x.match(patt1));

added '\' in the regx.

Upvotes: 0

rbginge
rbginge

Reputation: 906

You need to escape the d character to represent a digit as below:

var patt1=/\d{3}/gi;

Otherwise your regular expression will be looking for three occurences of the 'd' character

Upvotes: 2

Related Questions