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"
Related
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 days ago.
Improve this question
I have the following regex that I will use in Javascript eventually:
^(0|[1-9][0-9]*)([,;|\t]|(?:x) | {2,})
Given this sample text, I would like to get on the capture group 1 the quantity and on the capture group 2 the separator (either a comma, semicolon, pipe, tab, two or more spaces or a "x" followed by a single space):
2,Apples
3x Trees
2 Oranges
2x A3252893
15553x Moons
15553x Apples Test test
Regex101 example
Now, the question is about me wanting to match the single space as separator only if it is preceded by the "x" character, i.e. basically leaving out the "x" from the group. If I change the regex above by replacing the non-capturing group with the positive lookbehind, it doesn't work:
^(0|[1-9][0-9]*)([,;|\t]|(?<=x) | {2,})
Regex101 updated example
Why doesn't it match? Is that because it is used inside an alternation? And how can I fix that?
EDIT: OK, so positive lookbehind is not the correct solution because I didn't match any x before that. So how can I match the separators that I've listed above on the same numbered capturing group 2 without including the x but only the space (i.e. capture the space only if its preceded by an x while keeping the other variants as they are)?
The problem is that the look behind assertion for an "x" comes at a moment that the digits have been matched, so the previous character will always be a digit, not "x".
You can solve this by reading the "x" first (when it is there), before continuing. So insert x?
^(0|[1-9]\d*)x?([,;|\t]|(?<=x) | {2,})
On regex101
If you don't want to allow the "x" to be there when another delimiter (or two spaces) is used, then use a negative look behind assertion in those cases:
^(0|[1-9]\d*)x?((?<!x)[,;|\t]|(?<=x) |(?<!x) {2,})
On regex101
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 \\.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want Javascript regex for all strings that start with $$ and ends with $$
example:
$$hello$$
$$world$$
^\${2}.*?\${2}$
How this works: first, you look for two dollar signs, then, it allows any input, and then it looks whether the string ends with two dollar signs.
A pattern like this should work:
^\$\$.*\$\$$
This will match the beginning of the string (^) followed by $$, followed by zero or more of any character, followed by $$, and the end of the string ($). For example:
/^\$\$.*\$\$$/.test('$$hello$$') // true
If you're looking for a substring of a larger string which matches this pattern, use something like this:
\$\$.*?\$\$
This will match $$, followed by zero or more of any character, non-greedily, followed by $$. For example:
/\$\$.*?\$\$/.exec('print "$$hello$$"') // ['$$hello$$']
But for something this simple, you can just use plain old string manipulation:
var string = '$$hello$$';
var result = (string.substr(0, 2) == '$$') && (string.substr(-2) == '$$'); // true
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a following requirement to only allow capital letters and , in a javascript form . I am unsure on how to check for special characters and script tags . I have written the following code . I do not want to allow characters such as $,%,& etc .
var upperCase= new RegExp('[A-Z]');
var lowerCase= new RegExp('^[a-z]');
var numbers = new RegExp('^[0-9]');
if($(this).val().match(upperCase) && $(this).val().match(lowerCase) && $(this).val().match(numbers))
{
$("#passwordErrorMsg").html("OK")
}
Based on what you've given us, this may suit the bill. It will determine if any characters are not in character classes a-z, A-Z or 0-9 but note this will also treat é or similar characters as rejected symbols.
So if the value is 'test_' or 'test a' it will fail but it will pass for 'testa'. If you want it to accept spaces change the regex to /[^a-zA-Z0-9 ]/.
if(!/[^a-zA-Z0-9]/.test($(this).val())) {
$("#passwordErrorMsg").html("OK");
}
This may be helpful.
javascript regexp remove all special characters
if the only characters you want are numbers, letters, and ',' then you just need to whitespice all characters that are not those
$(this).val().replace(/[^\w\s\][^,]/gi, '')
This link may be helpful:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
It has a lot of information on JS Regexps.
For a dollar sign ($), the regexp syntax would be: \$. You need to escape the special character so it is read as a literal. The syntax would be the same for the other special characters, I believe.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
For my website , i need a REGEXP in java script for validation TITLE which can take alphabates, digits and Special char set [, / ( ) & - : . space], but if any user enter only single and double spaces or single or double .. like [..] in title or double digit [1 2] then it's should not allowed, atlest one aplhabate is required. please help
You can use this pattern:
^[-a-z0-9,/()&:. ]*[a-z][-a-z0-9,/()&:. ]*$
This will match any number of your special characters followed by a Latin letter, followed by number of your special characters. It's effectively equivalent to [-a-z0-9,/()&:. ]+ except it requires at least one [a-z] somewhere in the string.
Of course, you need to escape the \ when written as a regex literal in javascript, and you probably want to use the i flag for case-insensitive matching:
var pattern = /^[-a-z0-9,\/()&:. ]*[a-z][-a-z0-9,\/()&:. ]*$/i