Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 months ago.
Improve this question
The following JS regex is working as expected
/^(?:(?:\(?(?:00|\+)([1-4]\d\d|[1-9]\d+)\)?)[\-\.\ \\\/]?)?((?:\(?\d{1,}\)?[\-\.\ \\\/]?){0,})(?:[\-\.\ \\\/]?(?:#|ext\.?|extension|x)[\-\.\ \\\/]?(\d+))?$/i
But when I use this as a HTML 5 pattern I got this error:
Pattern attribute value /^(?:(?:(?(?:00|+)([1-4]dd|[1-9]d+))?)[-. \/]?)?((?:(?d{1,})?[-. \/]?){0,})(?:[-. \/]?(?:#|ext.?|extension|x)[-. \/]?(d+))?$/i is not a valid regular expression: Uncaught SyntaxError: Invalid regular expression: //^(?:(?:(?(?:00|+)([1-4]dd|[1-9]d+))?)[-. \/]?)?((?:(?d{1,})?[-. \/]?){0,})(?:[-. \/]?(?:#|ext.?|extension|x)[-. \/]?(d+))?$/i/: Invalid group
The browser telling me this "Uncaught SyntaxError: Invalid regular expression. Invalid group"
Any help would be really appreciated as regex is not my real strength.
A regex and a regex string are two different things.
Regex example:
/[a-zA-Z\d]+/.test('abc123')
Equivalent regex string example:
new RegExp('[a-zA-Z\\d]+').test('abc123')
A backslash in a string escapes the character that follows. For many characters it is a no-op, as in a '\d' string, which is equivalent to 'd'. Hence you need to specify a double backslash to get \d that can be used in a regex string to mean a digit.
Example use in HTML5 to validate an input:
<input type="text" name="uname" pattern="[a-zA-Z\d]+" minlength="4" maxlength="10" />
So in your case, make sure to escape the backslash in the regex string:
"^(?:(?:\\(?(?:00|\\+)...
In the pattern attribute you can't specify the modifier i to ignore case, e.g. you need to tweak the regex string itself to be case insensitive.
Docs on HTML pattern attribute:
https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/pattern
JS Regex input as copied from example:
/^(?:(?:\(?(?:00|\+)([1-4]\d\d|[1-9]\d+)\)?)[\-\.\ \\\/]?)?((?:\(?\d{1,}\)?[\-\.\ \\\/]?){0,})(?:[\-\.\ \\\/]?(?:#|ext\.?|extension|x)[\-\.\ \\\/]?(\d+))?$/i
HTML5 Regex error output as copied from example:
//^(?:(?:(?(?:00|+)([1-4]dd|[1-9]d+))?)[-. \/]?)?((?:(?d{1,})?[-. \/]?){0,})(?:[-. \/]?(?:#|ext.?|extension|x)[-. \/]?(d+))?$/i/
From the look of it, javascript is reading your backslashes as if they were escaping characters.
From JS example to HTML error:
\d\d becomes dd
Any single backslash without a recognized escape character like d, another backslash, etc. just gets deleted.
There are more examples if you look through the input and the resulting error.
If you are using JS to pass this pattern to the html DOM as a string, you need to escape the backslashes. Anytime your pattern needs a backslash, you need to put 2 of them in the string. The first backslash tells the interpreter that the second backslash is a part of the text and not an escape sequence. If the pattern is stored in a tag, like your example, and you are accessing it with JS, you would still want to escape escape your \ by replacing them with \\.
Related
I'm trying to do a search for a character in a string NOT matching the regex :
password.search(/[`!###$%^&*A-Za-z0-9]/i));.
Basically, all characters that aren't this regex isn't allowed and I want to know if the user has input any characters that isn't allowed. For example, '\', or any other characters that I might not think of.
I'm pretty sure there's a question similar to this out somewhere, but despite trying to look for it I surprisingly couldn't find it. If this is a duplicate question please link me.
According to this answer, you could use ?!:
console.log("valid$\\".search(/(?![`!###$%^&*A-Za-z0-9])/i));
console.log("256)128".search(/(?![`!###$%^&*A-Za-z0-9])/i));
f you want to exclude a set of characters (some punctuation characters, for example) you would use the ^ operator at the beginning of a character set, in a regex .
If I want to find a reference to precisely the following string:
http ://www.mydomain.com/home
within a more complex regex expression.
Is it possible to escape the whole sequence instead of escaping each / and . character individually? To get something more readable than
/http:\/\/www\.mydomain\.com\/home/
In the regex parsing site https://regexr.com/ , if I type the url in and set a regex to
/(http ://www.mydomain.com/home)/
, it appears to recognize the string, yet declares an error:
Unescaped forward slash. This may cause issues if copying/pasting this expression into code.
So I'm confused about this issue.
It appears that regex does not offer such a syntax, at least for Javascript. It is possible, however, to proceed as follows:
use a string and automatically escape all the special characters in it,
as indicated here: Javascript regular expression - string to RegEx object
concatenate that string with strings representing the rest of the expression you want to create
transform the string into a regex expression as indicated in Escape string for use in Javascript regex .
This question already has answers here:
AngularJS - Remove leading and trailing whitespace from input-box using regex
(2 answers)
Closed 7 years ago.
I am not very good at regular expressions so maybe this is a simple question, but I am certainly missing something. I use regular expressions to validate specific input from user. The input must be accepted (regex must match) if and only if the input string contains no commas and no whitespaces(in other words, the input must be single word without commas). Except that, it can contain any symbols and the input string can have any length. Now, when I use this regular expression, it matches input, that doesn't contain commas.
/^[^,]*$/
I wanted to add the whitespace part to it, so I made this expression
/^[^,\s]*$/
which behaves in a very weird way. It does what it should except one thing. For some reasons, it matches(and lets in) strings, that end with space (If they end with comma, everything is OK and it doesn't match). I dont wan't it to match strings with trailing whitespaces but I don't know, how to adjust the regular expression to do this. So my questions are - why is this weird thing happening and how to change the regular expression to do what it should.
here is an example:
http://jsbin.com/qoyoyagilo/2/edit?html,js,output
What is even weirder, when I tried my regex on rubular, it didn' t match strings with trailing whitespaces. I am starting to believe, that this has to do something with javascript and not with my particular regex
Angular already trims your strings before validating them and binding to model. Extra whitespace at the beginning and at the end of strings won't even be matched against your regular expression (or any other validator).
You can use ng-trim="false" if you wish to disable this behavior:
<input ng-model="yourmodelvar" ng-trim="false" ng-pattern="[^,\s]*">
Also note that you don't need the ^ and $ chars in your regexp, since validation is performed against the whole string automatically. From the documentation on ng-pattern:
Sets pattern validation error key if the ngModel $viewValue value does
not match a RegExp found by evaluating the Angular expression given in
the attribute value. If the expression evaluates to a RegExp object,
then this is used directly. If the expression evaluates to a string,
then it will be converted to a RegExp after wrapping it in ^ and $
characters. For instance, "abc" will be converted to new
RegExp('^abc$').
References:
https://docs.angularjs.org/api/ng/directive/input (official doc)
How to disable trimming of inputs in AngularJS?
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have a basic regex that should return string after the last backslash \.
Regex :
/([^\\]*$)/
Works fine in Regex101.
Output :
random.html
But not in Javascript example bellow :
console.log("C:\fakepath\extra\random.html".match(/([^\\]*$)/));
Output :
["C:akepathextra
andom.html", "C:akepathextra
andom.html", index: 0, input: "C:akepathextra
andom.html"]
The problem is not with the RegEx, it's with the string itself. In JavaScript strings \ is used to escape the following character.
The string
"C:\fakepath\extra\random.html"
is after escaping
C:akepathextra
andom.html
To use backslash in the string, escape them by preceding backslash.
"C:\\fakepath\\extra\\random.html"
console.log("C:\\fakepath\\extra\\random.html".match(/[^\\]*$/));
To get the text after last \, use String#split and Array#pop
"C:\\fakepath\\extra\\random.html".split('\\').pop() // random.html
^^ Note: this backslash also need to be escaped.
[^\\]* match a single character not present in the list.
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed
The problem is not with the RegEx, it's with the string itself. In JavaScript strings \ is used to escape the following character.
To use backslash in the string then escape them by preceding backslash.
"C:\\fakepath\\extra\\random.html"
I'm creating a regex expression to get the variables passed to a JavaScript constructor.
The input is always going to follow along these lines:
app.use(express.static('public'));
And the regex I plan to use to strip out the unnecessary parts is:
(^app.use\()|(..$)
The first part of the regex gets everything up to the first parenthesis, and the it's supposed to pipe it to another expression which gets the last 2 characters of the string.
My issue is that it seems to be ignoring the second regex. I tried a few other expressions in the second part and they worked, but this one isn't.
What am I doing wrong?
Regex example on Regex101: https://regex101.com/r/jV9eH6/3
UPDATE:
This is not a duplicate of How to replace all occurrences of a string in JavaScript?
My question is about a specific issue with a regex, not about replacing one string with another in JavaScript.
You need to use multiline modifier. Whenever anchors ^, $ are used in your regex then feel free to add multi-line modifier m.
/(^app.use\()|(..$)/gm
DEMO