Javascript regex for accepting only number without "," or "." character [duplicate] - javascript

This question already has answers here:
Regex to check whether a string contains only numbers [duplicate]
(21 answers)
Closed 5 years ago.
I'm looking for a regex javascript expression for allowing only numbers at this format 1234. Not 1,234 or 1.234. I'm having this regex now but it does not seem to work properly /[a-z]/i

var reg = /^\d+$/;
should do it. The original matches anything that consists of exactly one digit.

Related

Split by word not contained in parentheses [duplicate]

This question already has answers here:
How to split by commas that are not within parentheses?
(6 answers)
How to split string while ignoring portion in parentheses?
(5 answers)
Closed 3 years ago.
Here is the my string
var string = 'Title:(India OR America) OR Keyword:(Education)';
After splitting with "OR" it is showing
["Title:(India", "America)", "Keyword:(Education)"]
But what I need is the below one.
["Title:(India OR America)", "Keyword:(Education)"]
Could anyone please help on how to split the string to get the required result.
To achieve this you'll need to use a negative lookahead to only find the OR string where it's not contained in parentheses. Try this:
var input = 'Title:(India OR America) OR Keyword:(Education)';
var output = input.split(/(?!\(.*)\s?OR\s?(?![^(]*?\))/g);
console.log(output);

Check if first character of string is digit or whitespace with regex or alternative [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I want to check if a given string's first character is a word character/not a digit or white-space and return a boolean.
I'm trying to use regex for this. What I have is:
function isFirstIndexCharacter(str){
var regex = RegExp('w')
return regex.test(str[0]);
}
I always get false no matter what my input is. Thanks for any help on this.
You can do something like this -
function isFirstIndexCharacter(str){
return /^\w/.test(str[0]);
}

Javascript string split with regex multiple conditions [duplicate]

This question already has answers here:
Javascript: Split a string by comma, except inside parentheses
(7 answers)
Regex split string by spaces taking into account nested brackets
(2 answers)
Closed 5 years ago.
I'm trying to split string
x#x,a,b,c,d(some(other,arg2),p)#yyy into array
['x#x','a','b','c','d(some(other,arg2),p)#yyy'].
The problem i faced is that i cannot split just comma /\,/g since members that contains already commas would split also. Any ideas?

REGEX javascript - find two chars set [duplicate]

This question already has answers here:
What special characters must be escaped in regular expressions?
(13 answers)
Closed 5 years ago.
i've to find the sequence of chars ,. inside a json file for example: ":0,.0}},{", so what is the best fitted regex string for it?
i tried /(,.){1,}/g but it doesn't work well.
It's because . is a special character.
const r = /(?:,\.)+/g
console.log('abc,.,.,.def'.match(r)) //[",.,.,."]
you need to escape the .:
/,\./g

Javascript Regular Expression to match six-digit number [duplicate]

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 incorporate a regular expression i have used in the past in a different manner into some validation checking through JavaScript.
The following is my script:
var regOrderNo = new RegExp("\d{6}");
var order_no = $("input[name='txtordernumber']").val();
alert(regOrderNo.test(order_no));
Why would this not come back with true if the txtordernumber text box value was a six digit number or more?
You have to escape your \ when used inside a string.
new RegExp("\\d{6}");
or
/\d{6}/
Insert an extra "\" in your regexp.
You need to escape your backslash. It's looking for "\d", not digits.
So...
var regOrderNo = new RegExp("\\d{6}");

Categories

Resources