473,322 Members | 1,314 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Help with efficient RegExp replace() loop

I have the following code:

var rx = /{{(.+?)}}/i;
var expr = 'each {{word}} wrapped in {{curly}} {{braces}} in this
{{string}} needs to be {{replaced}} with a different {{value}}.';
var values = [];
values["value"] = "foo";
values["string"] = "bar";
values["braces"] = "bundy";

while(expr.match(rx)) {
expr = expr.replace(rx, values[RegExp.$1]);
}

Can anyone suggest a more efficient way of writing the match/replace
loop. Is there any way to do it with a single replace() call only?

Please note that I'm looking solely at improving the efficiency of the
while() loop. The actual input string and array of values are
irrelevant and for demonstration purposes only.

TIA

Will
Jul 20 '05 #1
1 1865
in**@whawes.co.uk (Will) writes:
I have the following code:

var rx = /{{(.+?)}}/i;
var expr = 'each {{word}} wrapped in {{curly}} {{braces}} in this
{{string}} needs to be {{replaced}} with a different {{value}}.';
Don't wrap code lines when posting. If your client can't avoid it,
make sure your lines are no longer than ~72 characters.
var values = [];
values["value"] = "foo";
values["string"] = "bar";
values["braces"] = "bundy";
You should use an object, not an array, for values. I.e.,
var values = {};
since you don't use the "arrayness" at all.
while(expr.match(rx)) {
expr = expr.replace(rx, values[RegExp.$1]);
} Can anyone suggest a more efficient way of writing the match/replace
loop. Is there any way to do it with a single replace() call only?
expr = expr.replace(/\{\{(.+?)}}/g,function(match,word){
return values[word] || "";
});
Remember that "{" is also special in regexps.

The || "" is for words that are not in the values dictionary. It returns
the empty string for those.
Please note that I'm looking solely at improving the efficiency of the
while() loop.


I don't know if it is more efficient, but it is slightly shorter. :)

Not all versions of Javascript allow a function as second argument, but
since you use the non-greedy match (.+?), which is also only in modern
browsers, I don't think it is a problem (i.e., if it fails, so does the
non-greedy match).

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Danny | last post by:
I need an asp command to strip out from a string all extra punctuation such as apostrophe, comma, period, spaces dashes, etc etc and just leave the letters. Can anybody give me some ideas? ...
5
by: Syed Ali | last post by:
Hello, I am trying to create a regexp to express non letters and space. I tried using: var myreg = new RegExp (""); However, it is not working. Basically I want to allow only words with...
3
by: MJ | last post by:
For some reason the following script does not work in Netscape/Mozilla, but works fine in IE and Opera. It is supposed to check the syntax, make sure there is a valid TLD (yes, those are all of...
4
by: Joseph | last post by:
The idea is to show only one of the <Baby_Div> while hiding all the others. At the moment all I have managed to do is to show each <Baby_Div> in turn as expected, but the problem is that once a...
4
by: Jon Maz | last post by:
Hi All, I want to strip the accents off characters in a string so that, for example, the (Spanish) word "práctico" comes out as "practico" - but ignoring case, so that "PRÁCTICO" comes out as...
7
by: iannorton | last post by:
Hi, I'm trying to write a RegExp that will return search an array and return the closest 10 matches, for example, if i entered "Hel" and my array contained "Hello", "Hell", it would return Hello...
9
by: Igal | last post by:
hay. i have read some topics about this. but there's something i don't quite understand, if anyone can help. i have text, in it i want to highlight the words user used to find this result. i...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.