I am using the following regex with bootstrapValidate plugin to validate a field.
bootstrapValidate('#input', 'regex:/^(my:track:[a-zA-Z0-9]{22}$)/:Invalid input')
The docs state to use this expression:
bootstrapValidate('#input', 'regex:^[a-z]+$:Please fulfill my regex')
The issue is I am using : inside the regex match and conflicts with the separator options.
How can I stop the : regex conflicting? Thanks!
http://www.w3processing.com/index.php?subMenuLoad=javascript/RegExp/RegExpUsing.php
This site says colon is a control character which must be escaped with a backslash (quoted below).
If you want to include any of these characters literally in a regular
expression, you must precede them with the backslash character \
(escape character).
var pattern1 = /\\/; \\ match backslash character
var pattern2 = /\[/; \\ match starting square brackets
var pattern3 = /\:/; \\ match colon character
Related
I'm not got, yet, with regex. I've been trying to break my head to get this to work.
I need a regex that allows the user to enter
Any alphabetical char (a-z)
Any number
For special char only "-" and "_".
"#" is not allowed.
I got this but no dice. [^a-zA-Z0-9]
Thanks
^[\w-]+$
will match a string following the rules you describe. \w matches letters, digits, or underscore, then it adds - to that set. Anchoring with ^ and $ requires all the characters in the string to match this pattern.
remove ^ character in square brackets because is negative ranges, add some \-\_ to allow '-' and '_' character inside square brackets
[a-zA-Z0-9\-\_]+
I have the following regex in javascript for a split operation since I can't do a negative look behind to find any delimiters , in a string that is not proceeded by one or more escape characters of \.
[^\\],
The regex works fine for finding where the commas not proceeded by \ are, but also finds the character that proceeds the comma as a match and thus splits the string incorrectly.
For example if I had the string
hello\,there,are
The result would be that e, matches my regex and not just ,. Making the split string array read
[hello\,ther] [are]
Why does the regex I am using keep finding the comma and the proceeding character instead of only matching the comma?
You cannot use split here because you'd need a lookbehind that JS regex does not support. Use a match with appropriate regex. Like the one below:
/(?:[^\\,]|\\.)+/g
See the regex demo.
The pattern matches 1 or more (+) sequences of any char other than , and \ ([^\\,]) or (|) any escaped character (excluding linebreak chars) with \\.
JS demo:
var regex = /(?:[^\\,]|\\.)+/g;
var str = "hello\\,there,are";
var res = str.match(regex);
console.log(res);
I am getting a string containing newlines (/n), tabs (/t) and lowercase letters [a-z]. It is possible to do that by matching /\n|\t/. AFAIK the dot represents the wildcard.
Therefore I was wondering, why /\n|\t/ doesn't match the same things as /\\./
var text = 'test1 \ntest2';
text.split(/\n/) //['test1', 'test2']
text.split(/\./) //['test1 \ntest2']
text.split(/\\./) //['test1 \ntest2']
Shouldn't the \\. match the \n (newline)?
Let me try and answer all the points:
AFAIK the dot represents the wildcard.
No, in regex, we do not use the term "wildcard". It is a special regex (meta)character. A dot in JavaScript regex matches any character but a newline.
I was wondering, why /\n|\t/ doesn't match the same things as /\\./
Because /\n|\t/ matches 1 symbol, either a newline or tab, while the regex /\\./ matches a literal \ and a character other than a newline.
The \n and \t are escape sequences. That means that the \ is not a literal backaslash that, together with the following symbol forms a code unit, a string that cannot be written otherwise. Indeed, how can we write a line break on the paper with a pen? No way!
See more about JavaScript character escape sequences here.
Now,
text.split(/\n/) //['test1', 'test2']
True, your input string contains a line break, thus, you get two elements in the resulting array
text.split(/\./) //['test1 \ntest2']
No match was found because \. matches a literal dot. A dot that is escaped (that has a literal \ before it) in the regex stops being a special regex metacharacter, and just matches its literal representation. Your string has no dot, thus, no matches.
text.split(/\\./) //['test1 \ntest2']
Again, no match is found, as /\\./ looks for a literal \ followed by any character but a newline.
A hint: use your expressions at regex101.com, it will tell you what your regex can match on the right.
Here, with regex, you have a literal notation (/.../). In literal notation, \ is considered a literal, thus, you do not have to escape it twice. If you used a constructor notation (i.e. RegExp(....)), you would have to use double escaping. E.g.
var re = /\\./; // is equal to
var re = new RegExp("\\\\.");
See more about constructor and literal notations at MDN RegExp help page.
\n gets evaluated to a new line, so you're essentially matching against an empty string. If you do a quick console.log('\n'); you can see the output of that.
I am using this regular expression: [a-zA-Z0-9\-.,:+*()=\'&_], but i am getting error like :'unterminated character class' error in this expression':
Demo Code:
Ext.getCmp('field2').addListener({
beforequery: function (e) {
if (e.query && e.query.indexOf('?') != -1) {
var temp = '';
for(var i=0;i<e.query.length;i++){
temp = temp + '['+e.query[i]+ ']';
}
e.cancel = true;
var query = new RegExp(String.format('^{0}',temp.replace(/\?/g, 'a-zA-Z0-9\.,:\+*()=\'&_-\\')));
this.expand();
this.store.clearFilter(true);
this.store.filter(this.displayField, query);
}
}
});
Errors:
1.Please someone tell me whats wrong in this, mainly with backslash.
2.when we enter desired characters in combobox they are being selected automatically..so when we want to enter new character we have to press side arrow or else remaining characters are being deleted...
Thanks once again,
Raj
I think you have to escape some of the items in your character class. Like your backslash, asterisk, plus, parenthesis and period.
Something like this [a-zA-Z0-9\\-\.,:\+\*\(\)=\\'&_]
Adding a backslash to special characters [\^$.|?*+(){} in a regular expression suppresses their special meaning which allows you to use them literally.
http://www.regular-expressions.info/reference.html
In the regex there are 11 characters you need to escape: the opening square bracket [, the backslash \, the caret ^, the dollar sign $, the period or dot ., the vertical bar or pipe symbol |, the question mark ?, the asterisk or star *, the plus sign +, the opening round bracket ( and the closing round bracket ).
You need to escape some characters in your regular expression. So it would look like:
var regex = /[a-zA-Z0-9\-\.,:\+\*\(\)=\\'&_]/; // Note the backslashes
Parentheses, the plus sign, the asterisk, and the backslash are some of the many characters that have a special meaning in regular expressions. In order to include them literally, you need to escape them with a backslash.
using http://www.regular-expressions.info/javascriptexample.html I tested the following regex
^\\{1}([0-9])+
this is designed to match a backslash and then a number.
It works there
If I then try this directly in code
var reg = /^\\{1}([0-9])+/;
reg.exec("/123")
I get no matches!
What am I doing wrong?
Update:
Regarding the update of your question. Then the regex has to be:
var reg = /^\/(\d+)/;
You have to escape the slash inside the regex with \/.
The backslash needs to be escaped in the string too:
reg.exec("\\123")
Otherwise \1 will be treated as special character.
Btw, the regular expression can be simplified:
var reg = /^\\(\d+)/;
Note that I moved the quantifier + inside the capture group, otherwise it will only capture a single digit (namely 3) and not the whole number 123.
You need to escape the backslash in your string:
"\\123"
Also, for various implementation bugs, you may want to set reg.lastIndex = 0;.
In addition, {1} is completely redundant, you can simplify your regex to /^\\(\d)+/.
One last note: (\d)+ will only capture the last digit, you may want (\d+).