0

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.

2
Contributors
13
Replies
15
Views
7 Years
Discussion Span
Last Post by GaneshKumar1508
0

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).

0

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?

0

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');

Edited by fxm: n/a

0

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?

0

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.

0

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.

0

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.

0

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

0

Hi fxm,

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

0

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 .

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.