-3

I am learning regex and I want to do regex matching. However, I can't escape single quote properly using code the below. So, I am getting " [ 'Hell\'o', 'World' ] ". Probably I can use /[^\s]+/g for the quick answer " [ 'Hell'o', 'World' ] " other than the code below. However, it's hard for me to understand how regex system works if I use it. Is it possible to use other regex matching tokens for escape single quote when it outputs?

function printWords(str) {
  var count = 0;
  var arr = str.match(/[\w’]+/g);
  console.log(arr);
  
}
printWords("Hell'o World ")

--

kimi Tanaka
  • 119
  • 2
  • 12
  • Maybe I don't understand what you want, but I'm getting this result, which looks fine to me: ["Hell'o", "World"] – zord Dec 04 '17 at 14:08
  • 1
    Possible duplicate of [Regex for quoted string with escaping quotes](https://stackoverflow.com/questions/249791/regex-for-quoted-string-with-escaping-quotes) – ctwheels Dec 04 '17 at 14:10
  • `Is it possible to use other regex matching tokens for escape single quote when it outputs` - The code you posted works as you described it. What is the issue? Do you have an input with an expected output that is not achieved? – Nope Dec 04 '17 at 14:14
  • If I understand what you're asking, this is much easier with `.replace` – kemotoe Dec 04 '17 at 14:14
  • @Fran I want to get “Hell’o” not using “/[^\s]+/g “. I want to add something into regex “/[\w’]+/g” for the answer. Thank you everyone. I will see the link which seems provide what I want to know! – kimi Tanaka Dec 04 '17 at 14:25
  • Ah, I see, you just want an alternative to your working code. In that case it might be better off on https://codereview.stackexchange.com/ ? – Nope Dec 04 '17 at 14:32
  • @Fran Thanks, I’ll try it there. I’m still new here. It’s also good to know this might not be on-topic here too. – kimi Tanaka Dec 04 '17 at 14:34

1 Answers1

1

Ok, I'm not sure if I understood you correctly if not please don't give mi minus :) But if I use your code and change single quote to normal single quote it works the same way as /[^\s]+/g. So I guess it's what you wanted. If not, then sorry :)

function printWords(str) {
  var count = 0;
  var arr = str.match(/[\w']+/g);
  console.log("/[\w']+/g\n", arr);
}
printWords("Hell'o World ");

function printWordss(str) {
  var count = 0;
  var arr = str.match(/[^\s]+/g);
  console.log("/[^\s]+/g\n", arr);
}
printWordss("Hell'o World ")

Regards, KJ

Krzysztof Janiszewski
  • 3,763
  • 3
  • 18
  • 38
  • I am still getting my same output as before on repl.it online editor, It should work as you are doing here. Anyway thanks a lot. – kimi Tanaka Dec 04 '17 at 15:30
  • So I guess the problem is in repl.it. It propably changes normal single quote to it's own and don't recognize it. – Krzysztof Janiszewski Dec 04 '17 at 15:38
  • If you're still interested I checked it on repl.it. And it works properly. ([repl](https://repl.it/repls/GeneralAthleticAmericancrayfish)) It displays `[ 'Hell\'o', 'World' ]` because the answer is printed in single quotes so backslash is an infrmation that single quote that's next to it is not a closing quote to the string "hell'o". I hope You understand what I mean :) – Krzysztof Janiszewski Dec 04 '17 at 17:43
  • Okay. So, Repl.it decided it to express like that. It’s confusing though.Here and there are different! Thanks. – kimi Tanaka Dec 04 '17 at 22:34