This question already has an answer here:
Why this javascript regex doesn't work?
(1 answer)
Closed 2 years ago.
I was very surprised that I didn't find this already on the internet.
is there's a regular expression that validates only digits in a string including those starting with 0 and not white spaces
here's the example I'm using
function ValidateNumber() {
var regExp = new RegExp("/^\d+$/");
var strNumber = "010099914934";
var isValid = regExp.test(strNumber);
return isValid;
}
but still the isValid value is set to false
You could use /^\d+$/.
That means:
^ string start
\d+ a digit, once or more times
$ string end
This way you force the match to only numbers from start to end of that string.
Example here: https://regex101.com/r/jP4sN1/1
jsFiddle here: https://jsfiddle.net/gvqzknwk/
Note:
If you are using the RegExp constructor you need to double escape the \ in the \d selector, so your string passed to the RegExp constructor must be "^\\d+$".
So your function could be:
function ValidateNumber(strNumber) {
var regExp = new RegExp("^\\d+$");
var isValid = regExp.test(strNumber); // or just: /^\d+$/.test(strNumber);
return isValid;
}
Related
This question already has answers here:
Difference between regex [A-z] and [a-zA-Z]
(6 answers)
Closed last month.
I want to disallow a string that has square brackets when using test method in regex.
function validateName(name){
var nameRegex = /^[A-zA-z\s][A-zA-z\s.-]{1,64}$/i;
console.log('##########Checking validation.........');
return nameRegex.test(name);
}
Try this regex and let me know:
/^[^\[\]]*$/
EDIT:
const regex = /^[^\[\]]*$/;
console.log(regex.test('ok')) // return true
console.log(regex.test('[ok')) // return false
console.log(regex.test('ok]')) // return false
This question already has answers here:
Including a hyphen in a regex character bracket?
(6 answers)
Closed 3 years ago.
I am asking you how to split a string using different separators and when the string is empty return just an empty space.
I don't know to combine both.
All I have is:
function split(string) {
var str = string.split(/[+-*]/);
return str;
}
Example:
split("este-es+otro*ejemplo"); // => ["este", "es", "otro", "ejemplo"]
split(''); // => [""]
Thank you.
Move the * at the first position inside square bracket ([]).
If any special character, such as backslash (*) is immediately after the left square bracket, it doesn't have its special meaning and is considered to be one of the characters to match literally.
Try /[*+-]/g
function split(string) {
var str = string.split(/[*+-]/g);
return str;
}
console.log(split("este-es+otro*ejemplo"));
This question already has answers here:
Parsing strings for ints both Positive and Negative, Javascript
(2 answers)
Closed 3 years ago.
I am trying to extract positive and negative integers from the given string. But able to extract only positive integers.
I am passing "34,-10" string into getNumbersFromString param
I am getting
Output:
['34','10']
The expected output should be
[34,-10]
How do I solve this problem?
function getNumbersFromString(numberString){
var regx = numberString.match(/\d+/g);
return regx;
}
console.log(getNumbersFromString("34,-10"));
You can also match the -sign(at least 0 and at most 1) before the number. Then you can use map() to convert them to number.
function getNumbersFromString(numberString){
var regx = numberString.match(/-?\d+/g).map(Number);
return regx;
}
console.log(getNumbersFromString("34,-10"));
Why use an regex here. What about split() and map()
function getNumbersFromString(numberString){
return numberString.split(',').map(Number)
}
console.log(getNumbersFromString("34,-10"));
Or is the input string not "clean" and contains also text?
You should use regex with conditional - symbol like that /-?\d+/, also you should convert string to number with e.g parseInt function.
RegEx:
This expression might help you to pass integers using a list of chars:
numbers 0-9
+
-
[+\-0-9]+
If you wish, you can wrap it with a capturing group, just to be simple for string replace using a $1:
([+\-0-9]+)
Graph:
This graph shows how the expression works:
Code:
function getNumbersFromString(numberString){
var regex = numberString.match(/[+\-0-9]+/g);
return regex;
}
console.log(getNumbersFromString("34, -10, +10, +34"));
Non regex version (assuming input strings are proper csv):
function getNumbersFromString(numberString){
return numberString.split(',').map(v => parseInt(v))
}
console.log(getNumbersFromString("34,-10"))
This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 4 years ago.
I am trying to match the list of symbols in regex but somehow the result is always returning with errors
symbol list = !##$+*{}?<>&’”[]=%^
if (text.match('^[\[\]\!\"\#\$\%\&\'\(\)\*\+\,\/\<\>\=\?\#\[\]\{\}\\\\\^\_\`\~]+$')) {
this.specialChar = true;
} else {
this.specialChar = false;
}
I am getting the following error:
Invalid regular expression: /^[[]!"#$%&'()*+,/<>=?#[]{}\\^_`~]+$/: Nothing to repeat
How do I correctly match the symbols in regex? basically I want to check if text contain any of those symbols.
You should use this regex constructor instead:
if (text.match(/^[\[\]\!\"\#\$\%\&\'\(\)\*\+\,\/\<\>\=\?\#\[\]\{\}\\\\\^\_\`\~]+$/)) {
this.specialChar = true;
} else {
this.specialChar = false;
}
The reason it fails is that you use a regex string constructor. If you still want to do that, you need to DOUBLE escape the characters, like this:
if (text.match('^[\\[\\]\\!\\"\\#\\$\\%\\&\\'\\(\\)\\*\\+\\,\\/\\<\\>\\=\\?\\#\\[\\]\\{\\}\\\\\\^\\_\\`\\~]+$')) {
Now you will create a valid regex.
This question already has answers here:
Escaping a forward slash in a regular expression
(6 answers)
Closed 8 years ago.
In my RadGrid I am using Filter for DATE Column. Date format is like this 16/12/1990. Filter textbox should allow only Numbers and /. How to write JavaScript function to do this?
function CharacterCheckDate(text, e)
{
var regx, flg;
regx = /[^0-9/'' ]/
flg = regx.test(text.value);
if (flg)
{
var val = text.value;
val = val.substr(0, (val.length) - 1)
text.value = val;
}
}
You don't have to worry about / in character class, (thanks Robin for pointing that out). For example,
console.log(/[^\d/]/.test("/"));
# false
console.log(/[^\d/]/.test("a"));
# true
If you are really in doubt, simply escape it with backslash, like this
regx = /[^0-9\/'' ]/
Also, you don't need to specify ' twice, once is enough.
regx = /[^0-9\/' ]/
Instead of using numbers explicitly, you can use \d character class, like this
regx = /[^\d\/' ]/
So, you could have written your RegEx, like this
regx = /[^\d/' ]/