-1) { alert(string); } Searching for "f", " /> -1) { alert(string); } Searching for "f", " /> -1) { alert(string); } Searching for "f", "/>

How to do indexOf √ (sqrt character)

55 Views Asked by At

How would I check whether a string contains the √ symbol?

var string = '√ foobar'
if (string.indexOf("f") > -1) { alert(string); }

Searching for "f", this finds the "f" in "foobar" fine. For what it's worth, the √ character does show as √ in the alert though: foobar

var string = '√ foobar'
if (string.indexOf("√") > -1) { alert(string); }

Searching for "√" doesn't do anything, no alert message.

2

There are 2 best solutions below

1
Mina On BEST ANSWER

It works for me

var string = '√ foobar'
if (string.indexOf("√") > -1) {
  alert(string);
}

But maybe the javascript environment which you are in, doesn't recognize the symbol, so I suggest you to try with the Unicode of that symbol. \u221A

var string = '√ foobar'
if (string.indexOf("\u221A") > -1) { alert(string); }

0
Jaredcheeda On
let example = '√ example';
console.log(example.includes('√'));

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes