firasdib / Regex101

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

JavaScript Code Generator: add functions for \U, \L, \E used in replacement string #1956

Closed MBM1607 closed 1 year ago

MBM1607 commented 1 year ago

Bug Description

The code generator for javascript creates faulty code when replacement with text case conversion is used

Example

const regex = /([aeiouy])(\s*)([bcdfghjklmnpqrstvwxz])/gmi;

// Alternative syntax using RegExp constructor
// const regex = new RegExp('([aeiouy])(\\s*)([bcdfghjklmnpqrstvwxz])', 'gmi')

const str = `hello world
xaabeuekadii
A quick brown fox jumps over the lazy dog`;
const subst = `$1$2\U$3\E`;

// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);

console.log('Substitution result: ', result);

Reproduction steps

Use \U or \L in substitution regex and then generate js code

Expected Outcome

The generator should ideally give the appropriate conversion function in js.

Browser

Firefox 108.0

OS

Pop!_OS 22.04 LTS x86_64

MBM1607 commented 1 year ago

I would be willing to take this up, but I couldn't find the js conversion tool source code.

working-name commented 1 year ago

Hello!

As far as I know this is not a feature implemented by the javascript regex engine (although there are workarounds). Are you proposing this as a new, site-only feature?

MBM1607 commented 1 year ago

Hi, I was proposing that the generator should also generate the relevant replacer function.

working-name commented 1 year ago

Got it 👍, so that's a feature request but it's external to javascript's regex implementation aka it doesn't know about \U yet.

firasdib commented 1 year ago

If you use the JS flavor it will show that this token is unsupported in that flavor.

The code generator does not do any conversions.