Regex invalid escape Javascript issue [duplicate] - javascript

I am trying to make an <input> field that uses a pattern to check if the input is a valid windows file path.
The pattern I have is
/^(?:[\w]\:|\\)(\\[a-zA-Z_\-\s0-9\.()~!##$%^&=+';,{}\[\]]+)+\.(exe)$/g
However, when putting this into the pattern attribute of an <input> field:
<input id="path" type="text" pattern="^(?:[\w]\:|\)(\[a-zA-Z_\-\s0-9\.()~!##$%^&=+';,{}[]]+)+\.(exe)$">
An error is displayed in the console saying:
Pattern attribute value
^(?:[\w]\:|\\)(\\[a-zA-Z_\-\s0-9\.()~!##$%^&=+';,{}\[\]]+)+\.(exe)$ is
not a valid regular expression: Uncaught SyntaxError: Invalid regular
expression: /^(?:[\w]\:|\\)(\\[a-zA-Z_\-\s0-9\.()~!##$%^&=+';,{}\[\]]+)+\.(exe)$/:
Invalid escape
I have tried several ways of writing this pattern, but none seem to work.

You only need to escape the characters in the character class that must be escaped otherwise, you will always get this error.
Use
pattern="(?:\w:|\\)(\\[\w\s.()~!##$%^&=+';,{}[\]-]+)+\.exe"
See the JSFiddle
Details:
The hyphen must be at the end of the character class
The ] inside the character class must be escaped
The [ and . must not be escaped
The : must never be esacaped, it is never a special character
[a-zA-Z0-9_] = \w
The pattern is always anchored by default, you need no ^ and $ anchors.

Related

Error Uncaught SyntaxError: Invalid regular expression: /(/: Unterminated group

I'm checking if my string has special characters to replace but for the following string I'm having the following problem
String
(Lot P Verdes)
Function
function retira_acentos(palavra) {
com_acento = 'áàãâäéèêëíìîïóòõôöúùûüçÁÀÃÂÄÉÈÊËÍÌÎÏÓÒÕÖÔÚÙÛÜÇ';
sem_acento = 'aaaaaeeeeiiiiooooouuuucAAAAAEEEEIIIIOOOOOUUUUC';
nova='';
for(i=0;i<palavra.length;i++) {
if (com_acento.search(palavra.substr(i,1))>=0) {
nova+=sem_acento.substr(com_acento.search(palavra.substr(i,1)),1);
}
else {
nova+=palavra.substr(i,1);
}
}
return nova.toUpperCase();
}
Error
line: if (com_acento.search(palavra.substr(i,1))>=0)
Uncaught SyntaxError: Invalid regular expression: /(/: Unterminated group
The problem you've stumbled across here is that String#search requires a regexp as input, you, however, seem to want to search for a string input, not a regexp. In that case, use String#indexOf instead.
Try changing these lines and see if it gives you the desired output:
if (com_acento.indexOf(palavra.substr(i,1))>=0) {
nova+=sem_acento.substr(com_acento.indexOf(palavra.substr(i,1)),1);
}
In regular expression, round parenthesis are used to define groups. In this case, the regex parser thinks that you opened a group but you forgot to close it.
You don't want to open a group, I guess. You just want to match the literal character.
To match literal characters, in javascript regex, you have two ways:
Escape the character with the backslace: \. Example: /\(/
Wrap your character in square brackets. Example: [(]
In your case, it would be preferrable to use the second approach, because it works with any character (even with the ones that don't need to be escaped) and works also with many characters.
So I advise you to change the parameter of search in this way:
search('['+palavra.substr(i,1)+']')

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.

invalid escape in pattern HTML/Javascript

I am trying to make an <input> field that uses a pattern to check if the input is a valid windows file path.
The pattern I have is
/^(?:[\w]\:|\\)(\\[a-zA-Z_\-\s0-9\.()~!##$%^&=+';,{}\[\]]+)+\.(exe)$/g
However, when putting this into the pattern attribute of an <input> field:
<input id="path" type="text" pattern="^(?:[\w]\:|\)(\[a-zA-Z_\-\s0-9\.()~!##$%^&=+';,{}[]]+)+\.(exe)$">
An error is displayed in the console saying:
Pattern attribute value
^(?:[\w]\:|\\)(\\[a-zA-Z_\-\s0-9\.()~!##$%^&=+';,{}\[\]]+)+\.(exe)$ is
not a valid regular expression: Uncaught SyntaxError: Invalid regular
expression: /^(?:[\w]\:|\\)(\\[a-zA-Z_\-\s0-9\.()~!##$%^&=+';,{}\[\]]+)+\.(exe)$/:
Invalid escape
I have tried several ways of writing this pattern, but none seem to work.
You only need to escape the characters in the character class that must be escaped otherwise, you will always get this error.
Use
pattern="(?:\w:|\\)(\\[\w\s.()~!##$%^&=+';,{}[\]-]+)+\.exe"
See the JSFiddle
Details:
The hyphen must be at the end of the character class
The ] inside the character class must be escaped
The [ and . must not be escaped
The : must never be esacaped, it is never a special character
[a-zA-Z0-9_] = \w
The pattern is always anchored by default, you need no ^ and $ anchors.

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 regexp - only if first character is not an asterisk

I am using a javascript validator which will let me build custom validation based on regexp
From their website: regexp=^[A-Za-z]{1,20}$ allow up to 20 alphabetic characters.
This will return an error if the entered data in the input field is outside this scope.
What I need is the string that will trigger an error for the inputfield if the value has an asterix as the first character.
I can make it trigger the opposite (an error if the first character is NOT an asterix) with:
regexp=[\u002A]
Heeeeelp please :-D
How about:
^[^\*]
Which matches any input that does not start with an asterisk; judging from the example regex, any input which does not match the regex will be cause a validation error, so with the double negative you should get the behaviour you want :-)
Explanation of my regex:
The first ^ means "at the start of the string"
The [ ... ] construct is a character class, which matches a single character among the ones enclosed within the brackets
The ^ in the beginning of the character class means "negate the character class", i.e. match any character that's not one of the ones listed
The \* means a literal *; * has a special meaning in regular expressions, so I've escaped it with a backslash. As Rob has pointed out in the comments, it's not strictly necessary to escape (most) special characters within a character class
How about ^[^\*].+.
Broken down:
^ = start of string.
[^\*] = any one character not the '*'.
.+ = any other character at least once.
You can invert character class by using ^ after [
regexp=[^\u002A]

Categories

Resources