Go4Expert

Go4Expert (http://www.go4expert.com/)
-   JavaScript and AJAX (http://www.go4expert.com/forums/javascript/)
-   -   invalid quantifier (http://www.go4expert.com/forums/invalid-quantifier-t19925/)

rekha 30Oct2009 18:17

invalid quantifier
 
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>


venami 30Oct2009 19:21

Re: invalid quantifier
 
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.

rekha 31Oct2009 12:16

Re: invalid quantifier
 
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.

venami 31Oct2009 14:23

Re: invalid quantifier
 
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.

rekha 31Oct2009 14:48

Re: invalid quantifier
 
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.

dennyphilip 31Oct2009 19:20

Re: invalid quantifier
 
usage for replace function
var str = "hi there";
str.replace("hi", "hello");

venami 1Nov2009 18:01

Re: invalid quantifier
 
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 '\+').

venami 1Nov2009 18:10

Re: invalid quantifier
 
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.

rekha 3Nov2009 18:38

Re: invalid quantifier
 
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.

rekha 3Nov2009 19:26

Re: invalid quantifier
 
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....


All times are GMT +5.5. The time now is 03:06.