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>
|
Contributor
|
|
| 30Oct2009,18:17 | #1 |
|
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>
|
|
Ambitious contributor
|
![]() |
| 30Oct2009,19:21 | #2 |
|
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>
SaswatPadhi, shabbir
likes this
|
|
Contributor
|
|
| 31Oct2009,12:16 | #3 |
|
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. |
|
Ambitious contributor
|
![]() |
| 31Oct2009,14:23 | #4 |
|
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. |
|
Contributor
|
|
| 31Oct2009,14:48 | #5 |
|
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. |
|
Light Poster
|
|
| 31Oct2009,19:20 | #6 |
|
usage for replace function
var str = "hi there"; str.replace("hi", "hello"); |
|
Ambitious contributor
|
![]() |
| 1Nov2009,18:01 | #7 |
|
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 '\+'). |
|
Ambitious contributor
|
![]() |
| 1Nov2009,18:10 | #8 |
|
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. |
|
Contributor
|
|
| 3Nov2009,18:38 | #9 |
|
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>
|
|
Contributor
|
|
| 3Nov2009,19:26 | #10 |
|
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.... |