Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

find the character "(" in a string in js

I have a string in my JS that may contain the character (M and I need to find their position – but I can’t work out how to without throwing an error I guess due to the bracket.

const findMTag = "(M"; //I need to search for this but it throws an error
let posMTag = nameJoined.search(findMTag);
console.log("TAG position =  " + posMTag);

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

String.search accepts a Regular expression as its argument, but you’re passing a string to it, so it doesn’t work. Take a look at String.search() for more info.

So your code will look like:

const findMTag = /\(M/;
const posMTag = nameJoined.search(findMTag);
console.log("TAG position =  " + posMTag);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading