0

Lets say I have the following string:

<div style="color: red; display.: inline-block;"><i class="fa fa-arrow-down" aria-hidden="true"></i> -18.9%</div>

What want to extract is -18.9

I currently have [^0-9.-]+ in javascript But that will include every dot and hyphen in the string, so I need something that only extract comma and hyphen if followed by a number.

And to clearify, the dots and hyphens may or may not exist, sometimes not the html either

Tronik
  • 47
  • 6

3 Answers3

2

Don't parse HTML with regex. Instead use a DOM parser, which makes it a piece of cake:

var str = '<div style="color: red; display.: inline-block;"><i class="fa fa-arrow-down" aria-hidden="true"></i> -18.9%</div>';
var parser = new DOMParser();
var doc = parser.parseFromString(str, "text/html");
// get text content and remove '%' from it.
console.log(doc.documentElement.textContent.replace(/%/, ''));

Note that this also works if str is just plain text, like 25.3.

If it represents a valid number you can convert it with parseFloat or the unitary +. Note that thousands separator(s) will need to be removed first with a replace.

If your actual HTML string is more complex and has other text nodes in it, then use the usual DOM methods to find the node of your interest, for instance:

doc.querySelector('div').textContent
Community
  • 1
  • 1
trincot
  • 317,000
  • 35
  • 244
  • 286
0

It sounds like you're dealing with a string, that appears to be taken from HTML. If this is the case, and you can't get the innerHTML property there are 2 options.

1. Convert to HTML and use innerText

var tempEl = document.createElement('div');
tempEl.innerHTML = '<div style="color: red; display.: inline-block;"><i class="fa fa-arrow-down" aria-hidden="true"></i> -18.9%</div>';

var myNumber = parseFloat(tempEl.children[0].innerText.trim())

will result in the number

-18.9

2. Regex This regex will match a string starting with a - and followed by a whole or decimal number

/(-)?\d+(\.\d*)*/g

Using your example:

<div style="color: red; display.: inline-block;"><i class="fa fa-arrow-down" aria-hidden="true"></i> -18.9%</div>

will result in a match of

-18.9

Here's a demo.

Brett DeWoody
  • 59,771
  • 29
  • 135
  • 184
  • Your regex almost works, but the hyphens may or may not exists – Tronik Dec 01 '16 at 13:52
  • Another question, sometimes the number will look like this: 19,079.50 will the comma be replaced with space or merged? Its the only thing that doesnt work right now. – Tronik Dec 01 '16 at 14:04
  • Could the whole regex above be inverted? to match everything but that. that way I can use replace method. – Tronik Dec 01 '16 at 14:07
  • I fixed it: [^\-*(\d+(\.\d*)?)] – Tronik Dec 01 '16 at 14:08
  • No I did not... [^\-(\d+(\.\d)?)] matches dots and hyphen elsewhere in the string – Tronik Dec 01 '16 at 14:10
  • Maybe you could provide more detail into what you're doing, it seems over-complicated. You want to replace the number within the tags? I would use option #1 or something like what @trincot recommended and use `innerText` to replace the number within the tag. – Brett DeWoody Dec 01 '16 at 14:10
  • 2
    Tronik, sooner or later your regex solution will break. Don't use regex on HTML. Developers commonly agree on that (out of experience). Think of what would happen if there are digits somewhere in the HTML tags (in their attributes). Do yourself a favour and [follow the advise of experienced developers](http://stackoverflow.com/a/1732454/5459839) – trincot Dec 01 '16 at 14:12
-1

here's the simple regular expression for it [-0-9]+.?[0-9]

noodlesegg
  • 149
  • 6