1

What is the necessary regular expression to match the € special character from the following html string?

€20.000.000,00

Ultimately, I want to strip the € symbol from the number. Currently, my code looks like this:

num = $("#number").html();
num.replace('€','');

$("number").html(num)

but of course I need to replace '€' with the correct regex.

4
  • replace() works with a regular string as well, so it should work. Commented Jul 20, 2011 at 23:20
  • I thought so, but it's not, so I figured I'd go for regex. Commented Jul 20, 2011 at 23:27
  • Well basically /\&euro\;/ should work as RegEx, haven't test it though so try it out Commented Jul 20, 2011 at 23:29
  • No, /\&euro\;/ doesn't work. Commented Jul 20, 2011 at 23:43

3 Answers 3

1

It appears that by the time you are manipulating the text with jQuery, the HTML abbreviation € has been replaced by its actual unicode value -- the true single character . A regular expression which refers to the unicode character value should do the trick for you.

var x = $('#number').html();
x = x.replace(/\u20ac/, '');
$('#number').html(x); 
Sign up to request clarification or add additional context in comments.

3 Comments

This did the trick. Do you know where/when the replacement occurs, and why?
I'm wondering if this will be consistent in other browsers, after reading jfriend00's answer (above).
I have verified that this code works in the following browsers: OS X Firefox 3.0, Windows Firefox 3.0-4.0, Windows IE 6.0-9.0, Windows Chrome 11. (Tested with Adobe Browserlab) The reason we have to work with the unicode character and not the HTML code is because we are dealing with the living Document Object Model which was created by, but different from, the HTML source code.
1

Doing regular expressions on HTML that has been extracted from a browser page is unreliable because the HTML you get back from the browser is often different in every browser. For example IE will remove quotes around attributes and change the order of attributes because it is reconstituting the HTML, not returning the original HTML. The same can be true of HTML entities (you may or may not get the actual entity). You can usually find a single piece of uninterrupted text, but anything more complicated than that can be troublesome.

It's always better to try to go up the food chain before it's been digested and regurgitated by the browser. Go to the actual page source where the browser hasn't manipulated anything yet or have the server put the desired value directly in a javascript variable for you (if possible).

Comments

0

.replace returns its result, it doesn't modify the string in-place.

2 Comments

I know. The next step is to do $("number").html(num) to replace the old string with the new. I'll edit the question to include this.
What I mean is do this: num = num.replace('€', '');.

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.