invalid quantifier

rekha's Avatar
Contributor
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>
0
venami's Avatar, Join Date: Dec 2008
Ambitious contributor
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, shabbir likes this
0
rekha's Avatar
Contributor
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.
0
venami's Avatar, Join Date: Dec 2008
Ambitious contributor
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.
0
rekha's Avatar
Contributor
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.
0
dennyphilip's Avatar, Join Date: Oct 2009
Light Poster
usage for replace function
var str = "hi there";
str.replace("hi", "hello");
0
venami's Avatar, Join Date: Dec 2008
Ambitious contributor
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 '\+').
0
venami's Avatar, Join Date: Dec 2008
Ambitious contributor
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.
0
rekha's Avatar
Contributor
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.
0
rekha's Avatar
Contributor
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....