firasdib / Regex101

This repository is currently only used for issue tracking for www.regex101.com
3.21k stars 198 forks source link

Whitespace character (\s) working in js where it should be \\s #2099

Closed AustinHeard closed 1 year ago

AustinHeard commented 1 year ago

Bug Description

A whitespace character (\s) working in js where it should be \s

Reproduction steps

image

When I use this expression in a js file it does not match but when I add the second \ it works

image

Expected Outcome

no matches

Browser

Chrome 114

OS

Pop!_OS 22.04 LTS

working-name commented 1 year ago

Hey @AustinHeard

This has to do with javascript's string processing. By the time \s gets to regex, it's just s because the \ before s escapes the character s itself. That's why when you add a second \ regex finally gets \\

Try it out:

'USBsCamera'.match(new RegExp('(?:USB\sCamera)'));
AustinHeard commented 1 year ago

Thanks for the reply!

I was just wondering if the expression field should require both / s if JavaScript is selected, or maybe when you copy it is copies with 2?

working-name commented 1 year ago

The site's regex input field is just regex, not a javascript string - and this applies across the board. There's no string processing in the style of the regex engine selected.

But you can get a properly escaped version of it to plop into JS if you click code generator in the left sidebar.

AustinHeard commented 1 year ago

Oh I didn't know that button was there. Thank you!