invalid quantifier

Discussion in 'JavaScript and AJAX' started by rekha, Oct 30, 2009.

  1. rekha

    rekha New Member

    Joined:
    Jan 17, 2008
    Messages:
    83
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    I got invalid quantifier error in my error console when i searched using javascript search.


    Code:
     <script type="text/javascript">
    
        var str="Javascript";
        document.write(str.search("C++"));
    
        </script>
    
     
  2. venami

    venami New Member

    Joined:
    Dec 26, 2008
    Messages:
    195
    Likes Received:
    10
    Trophy Points:
    0
    Occupation:
    Software Professional
    Location:
    India, Tamil Nadu, Cuddalore
    Home Page:
    http://mvenkatapathy.wordpress.com
    Hi rekha,

    The search function uses regular expressions for searching the string in a page.
    So '+' means something else(to be precise: "atleast one") in regular expression. To make search() to recognize your '+' as '+' and not something else, you have to escape that character.

    You can do like this:
    Code:
    <script type="text/javascript">
    
        var str="Javascript";
        document.write(str.search(/C\+\+/));
    
    </script>
    
    In this code, '/' denotes the start and end of regexp and '\' escapes the '+' sign and makes it behave like a character.
     
    SaswatPadhi likes this.
  3. rekha

    rekha New Member

    Joined:
    Jan 17, 2008
    Messages:
    83
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    Thanks for your reply.It works.But the data i am searching is fetched from database.So how can I split that particular value.
     
  4. venami

    venami New Member

    Joined:
    Dec 26, 2008
    Messages:
    195
    Likes Received:
    10
    Trophy Points:
    0
    Occupation:
    Software Professional
    Location:
    India, Tamil Nadu, Cuddalore
    Home Page:
    http://mvenkatapathy.wordpress.com
    Rekha,

    If I had understood your question correctly, you can use the str.replace() to replace the text that you get from the database(so that '+' sign is escaped) and use the result in the search() function.
     
  5. rekha

    rekha New Member

    Joined:
    Jan 17, 2008
    Messages:
    83
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    I know str_replace() will be useful but I don't know how to use it exactly because the values in the database will be,

    "Javascript,PHP,C++,JSP/Java,etc....."

    So for c++ we can use but for all others what to do.If the user enters a new category with special characters ,for that also I dont want to get the invalid quantifier error.

    So Pls help me..

    Thanks.
     
  6. dennyphilip

    dennyphilip New Member

    Joined:
    Oct 18, 2009
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    bengaluru
    usage for replace function
    var str = "hi there";
    str.replace("hi", "hello");
     
  7. venami

    venami New Member

    Joined:
    Dec 26, 2008
    Messages:
    195
    Likes Received:
    10
    Trophy Points:
    0
    Occupation:
    Software Professional
    Location:
    India, Tamil Nadu, Cuddalore
    Home Page:
    http://mvenkatapathy.wordpress.com
    Hi rekha,

    Now I got your doubt.

    Let us assume you have stored your content from database into variable "src" and the string that you are going to search in "src" is stored in variable "str".

    If you have special characters in src, you no need to worry. But if you have special characters in str, then escape them(ie: replace '+' with '\+').
     
  8. venami

    venami New Member

    Joined:
    Dec 26, 2008
    Messages:
    195
    Likes Received:
    10
    Trophy Points:
    0
    Occupation:
    Software Professional
    Location:
    India, Tamil Nadu, Cuddalore
    Home Page:
    http://mvenkatapathy.wordpress.com
    The special characters that you need to take care are \,^, ., [, ], -, |, +, *, ?, {, }, $, ?! and ?=

    If you are using these characters in your search string "str", then you have to escape them using '\'.

    Just let me know if you need any more information regarding your error.
     
  9. rekha

    rekha New Member

    Joined:
    Jan 17, 2008
    Messages:
    83
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    I have used the below code but it shows invalid quantifier error.

    Code:
    <html>
    <body>
    
    <script type="text/javascript">
    
    var str="c++";
    document.write(str.replace(/"+"/g,/"\+"/)); 
    var s=str.replace(/"+"/g,/"\+"/);alert(s);
    var sss=str.toString().search("\""+s+"\"");alert(sss);
    				  
    </script>
    </body>
    </html>    
    The string c++ is not replaced as "C\+\+".I don't know where is the issue.
     
  10. rekha

    rekha New Member

    Joined:
    Jan 17, 2008
    Messages:
    83
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    I have one more doubt.I am saving the value for eg "C++","Javascript" in a cookie.In the cookie it is stored as "C++","Javascript".But If I retrieve the cookie value using $_COOKIE['ss']; value is retrieved as "C\\ \\","Javascript".

    So I can't able to retrieve the cookie value.Anyone suggest me a good solution pls....
     
  11. venami

    venami New Member

    Joined:
    Dec 26, 2008
    Messages:
    195
    Likes Received:
    10
    Trophy Points:
    0
    Occupation:
    Software Professional
    Location:
    India, Tamil Nadu, Cuddalore
    Home Page:
    http://mvenkatapathy.wordpress.com
    Have you resolved the above two issues?
     
  12. rekha

    rekha New Member

    Joined:
    Jan 17, 2008
    Messages:
    83
    Likes Received:
    0
    Trophy Points:
    0
    Hi venami,

    I haven't solved the above two issues.Could you pls help me regarding this pls.
     
    Last edited: Mar 3, 2010

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice