Antje Hofmann: Regulärer Ausdruck und Variable *verzweifel*

Beitrag lesen

Hallo

http://forum.de.selfhtml.org/archiv/2000_1/t10237.htm#a51558

eval() ist eine Möglichkeit. Besser ist es aber, ein neues RegObjekt zu erzeugen:

xxx="irgendwas";
var such = new RegExp(xxx,"i");

Auszug aus der Doku:

The literal format is used as follows:

/pattern/flags
The constructor function is used as follows:

new RegExp("pattern"[, "flags"])
Parameters
pattern The text of the regular expression.
flags  If specified, flags can have one of the following values:

g: global match
i: ignore case
gi: both global match and ignore case

Notice that the parameters to the literal format do not use quotation marks to indicate strings, while the parameters to the constructor function do use quotation marks. So the following expressions create the same regular expression:

/ab+c/i
new RegExp("ab+c", "i")

Description
When using the constructor function, the normal string escape rules (preceding special characters with \ when included in a string) are necessary. For example, the following are equivalent:

re = new RegExp("\w+")
re = /\w+/

Viele Grüße

Antje