JavaScript RegExp objects
NickName:okoman Ask DateTime:2009-02-06T01:22:03

JavaScript RegExp objects

I try to write a simple Markdown parser in JavaScript. Therefore I want to check for the [link content][link id] syntax. I use the following code:

data = data.replace( /\[(.*?)\][ ]*\[([0-9]+)\]/g, '<a href="$2">$1</a>' );

It works well, but now I want to do this with a RegExp object. So I set up the following bit of code:

var r = new RegExp( '\[(.*?)\][ ]*\[([0-9]+)\]', 'g' );
data = data.replace( r, '<a href="$2">$1</a>' );

But it doesn't work. It even says that my regular expression (which works since the first example does a good job) is invalid:

unmatched ) in regular expression

I think it must have to do with some RegExp-object peculiarities I am not aware of. What am I doing wrong and how can the problem be solved?

Copyright Notice:Content Author:「okoman」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/516917/javascript-regexp-objects

Answers
bobince 2009-02-05T18:51:44

In addition to the pattern's backslash problem, this:\n\ndata = data.replace( r, '<a href=\"$2\">$1</a>' );\n\n\ncould be dangerous. I'll assume you've already taken care of the HTML-escaping, so I won't be able to do this:\n\n[<script>stealCookies()</script>][http://oops.example.com/]\n[hover me][http://hello\" onmouseover=\"stealCookies()]\n\n\nbut you'll still need to check the URL is a known-good scheme so I can't do this:\n\n[click me][javascript:stealCookies()]\n\n\nYou'll probably want to use the String.replace(r, func) variant of the method, and include validation in your replacement-making 'func'.",


chills42 2009-02-05T17:25:48

var r = /\\[(.*?)\\][ ]*\\[([0-9]+)\\]/g;\ndata = data.replace( r, '<a href=\"$2\">$1</a>' );\n",


Quassnoi 2009-02-05T17:26:01

Double escape you backslashes:\n\nvar r = new RegExp( '\\\\[(.*?)\\\\][ ]*\\[([0-9]+)\\\\]', 'g' );\n",


Sean Bright 2009-02-05T17:25:40

Because the first argument of the RegExp constructor is a string, not a pattern literal, you have to escape the backslashes, since you want literal backslashes in the pattern:\n\nvar r = new RegExp( '\\\\[(.*?)\\\\][ ]*\\\\[([0-9]+)\\\\]', 'g' );\n",


More about “JavaScript RegExp objects” related questions

regular expression javascript with RegExp

I read on RegExp in javascript And I see two ways to create new RegExp:/ab+c/i; new RegExp('ab+c', 'i'); new RegExp(/ab+c/, 'i'); But I want to create new RegExp like this: va

Show Detail

JavaScript RegExp objects

I try to write a simple Markdown parser in JavaScript. Therefore I want to check for the [link content][link id] syntax. I use the following code: data = data.replace( /\[(.*?)\][ ]*\[([0-9]+)\]/g...

Show Detail

Are /regex/ Literals always RegExp Objects?

Basically, my question is about how Javascript handles regex literals. Contrasting with number, string and boolean where literals are primitive data types and corresponding Number, String and Bool...

Show Detail

Is it preferred to use methods on RegExp objects or String objects?

There are regex matching methods on both RegExp objects and String objects in Javascript. RegExp objects have the methods exec test String objects have the methods match search The exec and ma...

Show Detail

How to modify a JavaScript RegExp pattern

I want to create a new RegExp object by modifying the pattern from another RegExp object. The output of RegExp.prototype.toString() includes leading and trailing slashes. // This is nice and ...

Show Detail

Is < intentionally ignored in Javascript RegExp as an assertion (as JSLint suggests)?

I ran JSLint on some inherited code, and received the following: Problem at line 24 character 36: Unexpected '\'. content = content.replace(/\&lt;a href=/g, '&lt;a target="blank" href='); I&

Show Detail

Serialization of RegExp

So, I was interested to find that JSON.stringify reduces a RegExp to an empty object-literal (fiddle): JSON.stringify(/^[0-9]+$/) // "{}" Is this behavior expected? I realize that a RegExp is an ...

Show Detail

JavaScript equivalent RegExp for PHP

Im looking for JavaScript equivalent RegExp for PHP. This PHP RegExp class must have same methods with same returns as JavaScript version. I can not find anything. Thanks [EDIT] I need same cl...

Show Detail

JavaScript RegExp

I thought I'd try and be clever with some JavaScript and use a regexp instead of repeated use of the indexOf method. I failed. Miserably. I would like to try and match anything (using the test met...

Show Detail

Javascript-Difference between RegExp Instance and RegExp Object

I have gone through Mozilla Developer Network Website for Deprecated and obsolete features in Javascript And found this line The following are now properties of RegExp instances, no longer of the

Show Detail