1

Say I have the following regex:

var myregex = /^(i|blah|ho)$/

I have another variable with:

var list = ["some","another"]

How do I make it such that I can append the words in list to someregex such that "myregex" results in:

var myregex = /^(i|blah|ho|some|another)$/
1
  • 1
    do you need a regex? It seems better to use indexOf Commented Jan 13, 2013 at 6:43

2 Answers 2

4

I would tend to try to find another way to approach the problem starting with the original list of words, but if you really need to append to an existing one, regular expression instances have a source property that returns the source of the expression as a string and a constructor that accepts a string. So you could do this:

var rex = /^(i|blah|ho)$/;
var list = ["some","another"];
rex = new RegExp(rex.source.replace(")$", "|" + list.join("|") + ")$");

That will lose the flags, so you may want to use the second argument to the RegExp constructor, building the "gi" or whatever string based on the global, ignoreCase, multiline, etc. properties of the original regex. (Or a hack for that is to use rex.toString() and grab whatever characters follow the last / in the string you get back, and provide those as the second argument.) So a more complete version of the above might be:

// (I added a flag to this one to check that the flags processing worked)
var rex = /^(i|blah|ho)$/i;
var list = ["some","another"];
var flags = rex.toString();
var index = flags.lastIndexOf("/");
flags = flags.substring(index + 1);
rex = new RegExp(rex.source.replace(")$", "|" + list.join("|") + ")$"), flags);

Live Example | Source

Also note that if your words have characters in them that are special to regular expressions (., \, /, $, etc.), the above would break. Alphanumerics are fine, otherwise you may want to use one of the various implementations of RegExp.escape that are out in the wild (sadly there is none in the standard) and use that to escape the individual words.

Sign up to request clarification or add additional context in comments.

4 Comments

Nice Hack! But still a hack.
Although this is a way to do so, I wonder why the OP does not thik about putting all the words into the regex from the beginning. Another potential problem with this method is that the list of words are not yet regex-escaped, so it may result in error if the word contains special character.
I am not totally sure, but I think it should be sufficient to add \ before these characters: ., \, |, *, ?, +, (, ), [, ], {, }, ^, $
@nhahtdh: Somewhere here on SO there's a long discussion of that... :-) Don't have time to go find it right now...
0

There are two approaches to doing this.

1.) via array concat

var myregex = /^(i|blah|ho)$/;
var list = ["some","another"];

var _newList = myregex.toString().match(/([a-z]+)/gi).concat(list);

var myNewRegEx = new RegExp("/^(" + _newList.join('|') + ")$/");

2.) via regex replace

var myregex = /^(i|blah|ho)$/;
var list = ["some","another"];

var _t = myregex.toString().replace(/\)/, '|' + list.join('|') + ')' );
var myNewRegEx = new RegExp(_t);

I don't know which way is faster.

Comments

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.