Regular expression with conditionals in Javascript error -> Invalid regular expression - javascript

I'm working on a regular expression that validates the pattern from A01 to A99.
I came out to this solution:
^A(?(?=0)0[1-9]|[0-9][0-9])$
Then, when I'm trying to implement it into JS I get an error that says
Uncaught SyntaxError: Invalid regular expression: /^A(?(?=0)0[1-9]|[0-9][0-9])$/: Invalid group
at new RegExp (<anonymous>)
at window.onload ((index):34)
I have tried doing:
new RegExp('^A(?(?=0)0[1-9]|[0-9][0-9])$');
Or
/^A(?(?=0)0[1-9]|[0-9][0-9])$/
But I get the same error with both of those. I know that is because of the conditional check inside de Regular Expression. Is there a way I can implement this pattern inside Js?
It is working on regex101: https://regex101.com/r/g0Qfac/1
Any ideas?
Thanks!

JavaScript regex does not support conditional constructs.
Use
^A(?:0[1-9]|[1-9][0-9])$
Or
^A(?:0[1-9]|[1-9]\d)$
Details
^ - start of string
A - a letter A
(?:0[1-9]|[1-9]\d) - either 0 followed with a digit from 1 to 9 or a digit from 1 to 9 followed with any single digit
$ - end of string.

Related

Regex Javascript - Invalid regular expression: Nothing to repeat for number input

I have this regex pattern /^-?+(\d+)?+([\.\,]?\d?+)/. It has to allow - sign for negative values at the start, also allows zero value and any numbers with one dot or comma only. I tested it on https://regex101.com/ and it works fine there however when I wanted to use in my React project i get this error and dont exactly know what cause this problem.
Module parse failed: Invalid regular expression: /^-?+(\d+)?+([.\,]?\d?+)/: Nothing to repeat (22:26)
+ is metacharacter in regex which acts as a quantifier, you need to escape it if you want to match + literally
[\] - Inside character class before , and . is not needed
You can update your regex to this
^[-+]?(\d+)(?:[.,]?\d+)?$
let nums = ["-1234", "+1232,1232", "+1234.12342,123"]
nums.forEach(num => {
console.log(/^[-+]?(\d+)(?:[.,]?\d+)?$/.test(num))
})
Regex Demo

Regex Invalid escape in HTML pattern Chrome [duplicate]

Im trying to create an html input tag that accepts only numbers entered in 1 of 2 formats, and reject all other input.
I want to accept numbers in these formats only, including requiring the dashes:
1234-12
and
1234-12-12
note: this is not for dates, but rather legal chapter numbers
Everything I am reading about regex says that the following should work, but it isn't.
<input class="form-control"
type="text"
pattern="^(\d{4}\-\d{2}\-\d{2})|(\d{4}\-\d{2})$"
required />
Devtools Console Error in Chrome:
Pattern attribute value ^(\d{4}\-\d{2}\-\d{2})|(\d{4}\-\d{2})$ is not a valid regular expression: Uncaught SyntaxError: Invalid regular expression: /^(\d{4}-\d{2}-\d{2})|(\d{4}-\d{2})$/: Invalid escape
You should not escape the hyphen outside a character class in ES6 regex used with the u flag (the one used by default in pattern regexps in the current versions of Chrome and FF).
Also, the regex in the pattern attribute is anchored by default, remove the redudant ^ and $ and shorten the pattern by using an optional group
pattern="\d{4}-\d{2}(-\d{2})?"
This regex in the HTML5 pattern attribute means:
\d{4}-\d{2} - match 4 digits, -, and then 2 digits from the start of string
(-\d{2})? - and optionally match a - and then 2 digits at the end of the string.

Valid looking regex pattern throws error when used with ng-pattern ?

I've found and want to use the following pattern ((ht|f)tp(s?)\:\/\/|~\/|\/)?([\w]+:\w+#)?([a-zA-Z]{1}([\w\-]+\.)+([\w]{2,5}))(:[\d]{1,5})?((\/?\w+\/)+|\/?)(\w+\.[\w]{3,4})?((\?\w+=\w+)?(&\w+=\w+)*)?
Regular expressions 101 considers it a valid regex for javascript.
Yet when I try to use it in my ng-pattern it throws the following error:
SyntaxError: Invalid regular expression: /^((ht|f)tp(s?)://|~/|/)?([w]+:w+#)?([a-zA-Z]{1}([w-]+.)+([w]{2,5}))(:[d]{1,5})?((/?w+/)+|/?)(w+.[w]{3,4})?((?w+=w+)?(&w+=w+)*)?$/: Invalid group
at new RegExp (native)
The exact way I'm implementing this is that I've defined the pattern in a configs file and then I'm loading it into the controller from which it is passed into the ng-pattern.
To use the regex inside a config, you need to store it as a string. Since JS engine treats strings as C strings with escape sequences, the backslashes that escape regex metacharacters must be doubled:
^((ht|f)tps?://|~/|/)?(\\w+:\\w+#)?([a-zA-Z]([\\w-]+\\.)+(\\w{2,5}‌​))(:\\d{1,5})?((/‌​?\\w+/)+|/?)(\\w+\\.\\w{3,4})?((\\?\\w+=\\w+)?(&\\w+=\\w+)*)?$
Also, I removed all unnecessary escaping: from /, from - in [\w-] (as at the end/start of the character class, - is treated as a literal hyphen), and removed unnecessary [...] from [\d] and [\w].
See the regex demo at regex101.com.

Javascript .search() issue Uncaught SyntaxError: Invalid regular expression: /*/: Nothing to repeat

Can anyone explain this bug for me , what we have here is :
if(statements[bracket].firsthalf.search(math_operators[j])!=-1)
where statements[bracket].firsthalf = "2*a" , math_operators[j]="*" , the console shows the following error:
Uncaught SyntaxError: Invalid regular expression: /*/: Nothing to
repeat
any idea why would it show such error ?
Use indexOf, not search. indexOf looks for literal strings, search is for matching a regular expression. In regular expressions, most punctuation characters have special meanings and need to be escaped if you want to find them literally, which is why you're getting errors.
Search need a RegularExpression as argument.
* is used to say 0 or more of the previous expression.
Like [0-9]* = 0 or more digits.
To use * as a character you have to escape it :
\*
You have to write the search part as a regular expression.
2*a".search(*) is non sense, because it doesn't search the character (*) but 0 or more time nothing because there is nothing before the *.
It's the same thing for the + that is protected character too.
You should use another function than search or write your request in a RegularExpression compliant manner like :
search([\*|\+|\-|\/])

How to replace compounds with special characters (e.g. +⃗)

I have a string with compound characters assembled of RegEx special characters. (e.g. (⃗ and +⃗ ). Now I want to replace them with something else using javascript on nodejs.
The problem is, that the interpreter thinks the + in the compound is a special character and throws this exception: SyntaxError: Invalid regular expression: /+⃗/: Nothing to repeat
Any ideas?
You can write your regex as
var regex = /\+\u20d7/; // for +⃗

Categories

Resources