Closed Bug 248062 Opened 20 years ago Closed 11 years ago

Add RegExp.escapeAsLiteralString (method on the constructor, not the objects it constructs)

Categories

(Core :: JavaScript Engine, enhancement)

x86
Windows XP
enhancement
Not set
normal

Tracking

()

RESOLVED WONTFIX

People

(Reporter: timeless, Unassigned)

Details

i have a 'simple' regexp task:

> i have a string (str)
> and it wants to be used in a replace function
> input.replace (magicfunction(str)+".*", "")
> problem is, str is something like '?'
> so it needs to be escaped :)

<shaver> if you're writing your own magicfunction, put together a set of
regexes that match the different characters you want to escape, and then
apply them in sequence
<shaver> [ [ /\\/, "\\\\" ], [/\?/, "\\?"], ... ]
<shaver> though you should toss a /g suffix on there as well
<shaver> but the better thing to do is have someone add RegExp.escapeAsLiteralString

The function RegExp.escapeAsLiteralString would of course return a string.
Mass abdication.
Assignee: shaver → nobody
QA Contact: pschwartau → general
Assignee: nobody → general
I'm not sure why I didn't suggest

str.replace(/[\\\?.:etc:]/g, "\\\1")

or an equivalent, rather than many regexen?
I would like to support this request.
Although I think maybe a simpler name for the method would be best (why not just RegExp.escape ? Possible conflict with an user-defined method ? Is this really worth it ?)
Also, it would be nice to have feedback from other implementors on this, the chosen solution should be implementable in a cross-browser way.
Should the ECMA or the WHATWG handle this kind of request instead ?
Please ask Ecmascript if you are still interested.
Status: NEW → RESOLVED
Closed: 11 years ago
Resolution: --- → WONTFIX
You need to log in before you can comment on or make changes to this bug.