Code Monkey home page Code Monkey logo

Comments (7)

jcalz avatar jcalz commented on July 17, 2024 2

"This is a crash" That means the compiler itself crashed. If the errors you're seeing are in your own code, or in code being compiled, it's not a crash. Only if the errors are a stack trace of the compiler code itself is it a crash.


This doesn't look like a bug to me. The problem is in /(\p{L}{1})\p{L}+/ itself. For that to be a valid regex it needs to have the u (or v) flag, as those unicode property value expressions are only supported in Unicode-aware mode. Certainly the equivalent code:

const re = /(\p{L}{1})\p{L}+/; // error
// later
let rgx = new RegExp(re, "gu");

should be an error, right? Because the thing you assigned to re is not valid. The fact that you later fix it doesn't change that. So are you saying there should be a carve-out for when invalid regexes are created inline? I'd think it would be better never to write /(\p{L}{1})\p{L}+/ in the first place, and just write /(\p{L}{1})\p{L}+/u, at least, which makes it valid. Once you do that the error goes away:

new RegExp(/(\p{L}{1})\p{L}+/u, "gu"); // okay

Of course then there doesn't seem to be much benefit over writing /(\p{L}{1})\p{L}+/gu in the first place. Indeed, making a regex only to throw it away while building another regex seems weird to me. If I saw someone write const x = [...[1, 2, 3], 4] instead of const x = [1, 2, 3, 4], I'd be very puzzled. Seems like the same thing here.

from typescript.

nmain avatar nmain commented on July 17, 2024 2

@anthonyLock I think the RegExp constructor is primarily for creating dynamic regexes, whose content is not known at compile-time. Situations like this:

function escape(s: string) {
	return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
const searchValue: string = getSearchValueFromInputField();
const escaped = escape(searchValue);
const testRegex = new RegExp(escaped, "i");

from typescript.

nmain avatar nmain commented on July 17, 2024 1

if you change the target to es5 then you get the error This regular expression flag is only available when targeting 'es6' or later.

That is correct. These features are not available in es5. If this works at runtime:

new RegExp(/(\p{L}{1})\p{L}+/, "gu").test('AB') === true

then the JS engine you are targeting is not es5.

and will cause confusion for others

I disagree. The regex /(\p{L}{1})\p{L}+/, without the u flag, is extremely confusing:

/(\p{L}{1})\p{L}+/.test('p{L}p{L}}}}}}') === true

from typescript.

nmain avatar nmain commented on July 17, 2024

Why not just write this as /(\p{L}{1})\p{L}+/gu?

from typescript.

anthonyLock avatar anthonyLock commented on July 17, 2024

@nmain if you change the target to es5 then you get the error This regular expression flag is only available when targeting 'es6' or later. Its not effecting me right now as I have changed it for the string approach let rgx = new RegExp('(\\p{L}{1})\\p{L}+', 'gu') but its a bug regardless, and will cause confusion for others.

from typescript.

RyanCavanaugh avatar RyanCavanaugh commented on July 17, 2024

Agree with prior commentors.

There's also the option of new RegExp("(\\p{L}{1})\\p{L}+", "gu")

from typescript.

anthonyLock avatar anthonyLock commented on July 17, 2024

@jcalz @nmain

Thanks for the information. I have closed the issue, if an admin wants to delete the issue feel free. I didn't realise that regex in that form was not supported in es5. Somewhere in my project between the setup and use of babel,nextjs etc... there is a configuration issue.

Quick question just to improve my own knowledge, please could you explain the use case of using the second variable in the constructor for RegExp and when would you would you want to use it instead of adding it into the first input?

from typescript.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.