Find and replace all prices in html

Can anyone help me with the js to find and replace any prices within the innerHTML?

Matching eg:

€25.00
$15
£1000.45
¥1000,999

i wan’t to find all instances of these and replace them with alternative strings

Hi Skyline,

Here is a simple example of javascript using innerHTML

<html>
<head>

<script type=“text/javascript”>
function change(){
document.getElementById(“header”).innerHTML=“New Header”;
}
</script>

</head>
<body>

<h1 id=“header”>Old Header</h1>
<a href=“#” onclick=“change()”>click me</a>
<p>“Old Header” was changed to “New Header”</p>

</body>
</html>

This should show how you can change the value if you can access the element. In your case is there a way you can get prices. Does this help?

Thanks for the reply.

Thing is i’m looking for the way to find all of the prices on the page so they can then be changed. A regex of some description i guess?

Probably. I dont know. But here is a simple sample that changes the prices if you know the value atleast.

<html>
<head>
<script type=“text/javascript”>
function searchPage(){
var bodyText = document.body.innerHTML;
document.write(bodyText.replace(“$15”,“$300”));
}
</script>
</head>
<body>
<button onclick=“searchPage()”>Click me</button>
<br/>
€25.00
<br/>
$15
<br/>
£1000.45
<br/>
¥1000,999
</body>

</html>

Mmm unfortunately the prices are unknown! Anyone good with js regex?

What are the currency formats you expect to be dealt with?

Is it only euro, dollar, pound and yen, in the formats that you provided, that you want to match?

€25.00
$15
£1000.45
¥1000,999

I’m no expert on regular expressions, but I think you might be after something like this:


/[€\\$£¥](\\d+(\\,\\d+)*|(\\d+))(\\.\\d+)?/

Yes that seems to find them!
Now how would i go about replacing each of the occurences with an ajax response?

By using http://api.jquery.com/jQuery.get/ or if you want to DIY, investigate Bulletprof Ajax