Finding different type of single quotes in javascript


Finding different type of single quotes in javascript



My requirement: There are three strings with single quotes of different types(like -> '‘’) and when I search with a single quote(i.e -> ') I should get the index of the single quote in all the cases.



e.g:


var str1 = "this is someone's question"
var str2 = "this is someone‘s question"
var str3 = "this is someone’s question"



str.indexOf("'"); -> This statement should find single quotes (', ‘ and ’) in all the three variables. Just like the google chrome find works in web page search.
Thanks in advance


str.indexOf("'");





they're different characters. you will need 3 statements or regex
– Philipp Sander
Jun 29 at 11:03





Google likely either normalizes this, or just searches for the different quotes.
– Dave Newton
Jun 29 at 11:03





.search(/[‘'’]/)
– Keith
Jun 29 at 11:07


.search(/[‘'’]/)




2 Answers
2



var index = /['‘’]/.exec(str3).index


var index = /['‘’]/.exec(str3).index



Using regex & .search() you can detect the single-quotes:


.search()




const regex = /('|‘|’)/gm;
var str1 = "this is someone's question"
var str2 = "this is someone‘s question"
var str3 = "this is someone’s question"

console.log("Found single-quote @ index: " + str1.search(regex));
console.log("Found single-quote @ index: " + str2.search(regex));
console.log("Found single-quote @ index: " + str3.search(regex));






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

Opening a url is failing in Swift