HI to all,

I am in need of escaping the regular expression special characters like '/', '.', '*', '+', '?', '|','(', ')', '', '{', '}', '\\'.

I have try with this by the following javascript but i can not achieve that.

RegExp.escape=function(str)
            {
                if (!arguments.callee.sRE) {
                    var specials = [
                        '/', '.', '*', '+', '?', '|',
                        '(', ')', '[', ']', '{', '}', '\\'
                    ];
                    arguments.callee.sRE = new RegExp(
                    '(\\' + specials.join('|\\') + ')', 'gim'
                );
                }
                return str.replace(arguments.callee.sRE, '\\');   
           
                }
                function regExpFind() {
//            var regex = new RegExp("\\[munees\\]","gim");
                  var regex=RegExp.escape("[Munees]waran");
                    alert("Reg : "+regex);          
                    alert(regex.exec("[Munees]waran"));
                }

If i go with the line which is commented,

var regex = new RegExp("\\[munees\\]","gim");

I have the correct output.
But by with the replace method i cannot achieve that?
Please guide me to achieve that.

var regex=RegExp.escape("[Munees]waran");

To generate a RegExp you need to change that to

var regex= new RegExp(RegExp.escape("[Munees]waran"));

Whether the resulting RegExp will do what you are trying to do isn't clear (to me, anyway).

Hi fxm,

I will try your code.But it's not give the output what i expect.Is there any other way to achieve this?

What is the input?
What output are you expecting?
What are you getting?

If the problem is that you are expecting (after my prior fix) that the first alert will show this Reg : /\[Munees\]waran/ but it is showing this Reg : /\Munees\waran/ instead, then the cure is to change line 12 as follows

return str.replace(arguments.callee.sRE, '\\$1');

Hi fxm,

Thanks for your efoorts.And i have update my code with your assistance but i got the following one,

Reg : /[Munees]waran/ instad of
  Reg : /\[Munees\]waran/

Is there any other way to achieve this?

Any other way to achieve what?

Hi fxm,

Thank you once again for your reply.

I need to get the output as

Reg:/\[Munees\]waran/

But i got

Reg:/[Munees]waran/

so i need to achieve /\[Munees\]waran/.So please help to get the expected output?

Thanks again.

If you applied each of the fixes I gave you, you would be getting what you need.

Please post the code you are now using.

Hi fxm,

Here i post my code for your reference,

<%@page contentType="text/html" pageEncoding="ISO-8859-9"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-9">
        <title>JSP Page</title>
        <script type="text/javascript">
            /**
             * Function for escape regular expression special characters.
             */
            RegExp.escape=function(str)
            {
                if (!arguments.callee.sRE) {
                    var specials = [
                        '/', '.', '*', '+', '?', '|',
                        '(', ')', '[', ']', '{', '}', '\\'
                    ];
                    arguments.callee.sRE = new RegExp(
                    '(\\' + specials.join('|\\') + ')', 'gim'
                );
                }
                return str.replace(arguments.callee.sRE, '\\$1');

            }
function regExpFind() {         
                    var regex= new RegExp(RegExp.escape("[Munees]waran"));
                    alert("Reg : "+regex);
                    alert(regex.exec("[Munees]waran"));
                }
        </script>
    </head>
    <body>
        <input type="button" value="Click" onclick="regExpFind()"/>
    </body>
</html>

I have got the output as Reg:/[Munees]waran/
But i need to get /\[Munees\]waran/
Please guide me to achieve this.
Thanks once again for all your efforts.

The first alert in the code you just posted displays Reg : /\[Munees\]waran/ What is the problem?

Hi fxm,

This first alert only displayed only
Reg: /[Munees]waran/
The second alert displayed null

Those outputs are from your original page with the errors.
The outputs from the corrected page you just posted are Reg : /\[Munees\]waran/ and [Munees]waran .

Hi fxm,

Can you post that code?Because i don't get the mentioned one.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.