# Javascript search for a special character

**URL:** <https://discourse.nodered.org/t/javascript-search-for-a-special-character/21428>\
**Category:** General\
**Created:** [8 February 2020 21:53 UTC](https://discourse.nodered.org/t/javascript-search-for-a-special-character/21428 "2020-02-08T21:53:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pvklink](https://avatars.discourse-cdn.com/v4/letter/p/46a35a/32.png) [@pvklink](https://discourse.nodered.org/u/pvklink)\
**Post date:** [8 February 2020 21:53 UTC](https://discourse.nodered.org/t/javascript-search-for-a-special-character/21428/1 "2020-02-08T21:53:33Z")

</div>

I am struggling for hours with my flow.  
Last part is to search for a [ sign in a variable and if not add it  
I made a simple test, but still get errors  
"SyntaxError: Invalid regular expression: /[/: Unterminated character class"

script  
var str = "{dit is een test}";  
var n = str.search("[");

if (n !== -1)  
{  
msg.a = "aap"  
}  
else  
{  
msg.a = "noot"  
}  
return msg;

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [8 February 2020 22:07 UTC](https://discourse.nodered.org/t/javascript-search-for-a-special-character/21428/2 "2020-02-08T22:07:09Z")

</div>

As it states your search criteria isn't a regular expression.

Use string.includes instead...

[https://www.w3schools.com/jsref/jsref\_includes.asp](https://www.w3schools.com/jsref/jsref_includes.asp)

---

<div class="post-metadata">

**Author:** ![Christian-Me](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/christian-me/32/10774_2.png) [@Christian-Me](https://discourse.nodered.org/u/Christian-Me)\
**Post date:** [8 February 2020 22:08 UTC](https://discourse.nodered.org/t/javascript-search-for-a-special-character/21428/3 "2020-02-08T22:08:56Z")

</div>

Perhaps better use string.indexOf() instead. This will give you the position of your search string.  
But as you are looking only for the existence the other solution will work too (but the result is a boolean value!)

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [9 February 2020 09:03 UTC](https://discourse.nodered.org/t/javascript-search-for-a-special-character/21428/4 "2020-02-09T09:03:33Z")

</div>

Hallo Peter,  
Om bovenstaande samen te vatten: `str.search` maakt gebruik van regex: regular expressions. `str.indexOf`/`str.includes` is een gewone zoekvorm, zoals je waarschijnlijk wilt.  
Regex wordt eigenlijk pas interessant als je op zoek bent naar specifieke combinaties. Daar komt bij dat het niet de meest leesbare taal is, die bovendien gebruik maakt van speciale tekens voor zoekopdrachten. Bijvoorbeeld het haakje waar je naar zoekt betekent in regex de start van een blok van toegestane tekens. En de error in je post geeft dan ook aan dat dit blok niet wordt gesloten. Voorlopig niets om je zorgen over te maken, gebruik de andere functies die genoemd worden, regex is hier niet nodig.  
M.v.g,  
Lena

—————  
To summarise the above posts, str.search uses regex, and in regex the brackets have a special meaning. Rather than searching for the exact character “[“ it will open a block of allowed characters, one that is never closed and thus gives the error. The other functions mentioned, includes and indexOf do the actual text search you think str.search does. So use either of those for your needs.

---

<div class="post-metadata">

**Author:** ![pvklink](https://avatars.discourse-cdn.com/v4/letter/p/46a35a/32.png) [@pvklink](https://discourse.nodered.org/u/pvklink)\
**Post date:** [9 February 2020 14:13 UTC](https://discourse.nodered.org/t/javascript-search-for-a-special-character/21428/5 "2020-02-09T14:13:03Z")

</div>

Thanks to you all!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [23 February 2020 14:13 UTC](https://discourse.nodered.org/t/javascript-search-for-a-special-character/21428/6 "2020-02-23T14:13:15Z")

</div>

Closed on 23 February 2020 14:13 UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
