Comments (7)
"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.
@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.
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.
Why not just write this as /(\p{L}{1})\p{L}+/gu
?
from typescript.
@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.
Agree with prior commentors.
There's also the option of new RegExp("(\\p{L}{1})\\p{L}+", "gu")
from typescript.
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)
- `abstract class` should be usable in expressions HOT 3
- TS 5.5.2: JavaScript and TypeScript language service has been disabled after crashing repeatedly HOT 1
- Object.assign() inconsistent overload: assign(target: object, ...sources: any[]): any; HOT 15
- C extends A but C does not extend A | B HOT 3
- Compiler Option typeTruncationLength
- Include paths evaluate to no files when tsconfig.json is in a sandbox (regression in 5.5) HOT 9
- Unpredictable behavior when trying to infer a generic type from a union function parameter
- Generic parameter type checking stops too early HOT 9
- TS 5.5 - Imports used by decorator output are removed HOT 2
- this.constructor.staticMethod() is ok in JS, but is an error in TS HOT 1
- Incorrect transform when namespace is empty HOT 1
- Filter with a type guard fails to narrow when the type guard's predicate is a tuple supertype HOT 4
- Declaration emit change in 5.5 with `exactOptionalPropertyTypes` HOT 3
- typing for navigator.clipboard HOT 5
- Investigate Wildcard Matching Performance HOT 2
- How to correctly obtain the type of return value after the decorator HOT 1
- The type reduction of the in operator is invalid in ts 5.5 HOT 7
- `getTypeListId` crashes with `TypeError: Cannot read properties of undefined (reading 'id')`
- Suggestion: A use case requires the "nameof" operator HOT 3
- Overload call signature resolution resolves to first overload HOT 11
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typescript.