String replacement problem

A

andrewflanders

I have an associative array of keys and values. I want to search a
string for the existance of keys and replace them with the values in
the array. The problem is that some of the keys resemble regular
expressions but they are not so if I build the regular expression with
a constructor it causes errors saying that the regular expression
syntax is bad. I got around this problem by skipping the regular
expression method and passing in the key directly as the first argument
of replace however this means I lose the ability to pass the /g global
switch to the replace function so it only replaces once.

So my question is: is there a way to incorperate a variable into a
regex without enterpreting the variable as a regex itself or
alternately is there a way to make the replacement global without using
a regular expression?
 
R

RobG

I have an associative array of keys and values. I want to search a
string for the existance of keys and replace them with the values in
the array. The problem is that some of the keys resemble regular
expressions but they are not so if I build the regular expression with
a constructor it causes errors saying that the regular expression
syntax is bad. I got around this problem by skipping the regular
expression method and passing in the key directly as the first argument
of replace however this means I lose the ability to pass the /g global
switch to the replace function so it only replaces once.

So my question is: is there a way to incorperate a variable into a
regex without enterpreting the variable as a regex itself or
alternately is there a way to make the replacement global without using
a regular expression?

Do you have a small example to illustrate? Special characters within
regular expressions can be quoted to remove their special-ness,
however I'm not sure they are suitable to use as keys for the
elements of an array.
 
D

drWot

If the string contains something (usually punctuation or control characters)
that has a special meaning when converted to a regular expression, you have
to escape it ('.' becomes('\.') before the string is converted.

This involves looking at each character in each string and testing it
against a list of suspects [\.\+\=\(\)\&\#\^\$\!\?].

Or you can make each string a new RegExp when you populate the array, and
make a
String.prototype.convertToRegExp() method to escape characters based on
their charCode.

It would be a little less elegant and a bit more efficient to use string
methods.

You can fake a global replace replace by running a regular replace through a
"while" loop:

while(str.indexOf('badString')!=-1)str=str.replace('rString','someotherString').

Both methods work, both have pros and cons.
 
L

Lasse Reichstein Nielsen

I have an associative array of keys and values. I want to search a
string for the existance of keys and replace them with the values in
the array. The problem is that some of the keys resemble regular
expressions

Ok, so if you want to use regular expressions for the search, the
keys need to be escaped (i.e., characters meaningfull to a regular
expression must be prefixed with a backslash).

Try this:
---
function replace(string, map) {
var i=0;
var keys = [];
for(var key in map) {
keys[i++] = key.replace(/([\\.+*?()[{|^$])/g,"\\$1"); // escape chars
}
var re = new RegExp(keys.join("|"),"g");
return string.replace(re, function(key) { return map[key]; });
}
---
(this uses the RegExp way of handling overlapping matches, i.e., if you
match for /foo|ok/g then "fook" only matches "foo" and not the overlapping
"ok").
So my question is: is there a way to incorperate a variable into a
regex without enterpreting the variable as a regex itself

Yes, escape character meaningfull in a RegExp
or alternately is there a way to make the replacement global without
using a regular expression?

Probably, but not easier in any way.

/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top