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 7 months ago.
The community is reviewing whether to reopen this question as of 6 months ago.
Improve this question
I am struggling with a Regex for this format [40]*100+ where the user can enter up to 3 digits inside the square brackets and up to 3 next to the multiplication sign. The plus sign can only appear if the previous format of [40]*100 is respected (the plus is optional at the end). And if they extend the input they should be able to extend that format like this [40]*100+[20]*100+[40]*100+ and so on.
This determines if the input is valid but trying to control the format escapes me.
function isValid2(str) {
return !/[~`!##$%\^a-zA-Z&()=\-\\\';,/{}\\":<>\?]/g.test(str);
}
$('input.cut_text').on('input change keydown', function () {
if (isValid2(this.value) == false){
this.value = this.value.replace(/[^1-9]/g,'');
return false
}
});
This regexp tests for the format you describe:
/^(\[\d{1,3}\]\*\d{1,3}\+)+$/
\d{1,3} matches up to 3 digits. We put one of these inside literal [], with literal * after that, and literal + after the second one. Then we use a quantified group to allow multiple repetitions.
You can't do the validation until the user has finished entering the field, because it won't match the partial value while they're typing. So you can use it in a change event listener, but not input or keydown.
Related
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 21 hours ago.
The community is reviewing whether to reopen this question as of 10 hours ago.
Improve this question
I'm trying to validate an input with regEx in Vue, which I don't have any idea how to make one and couldn't find online how to match what I want to do.
The thing is I'm trying to validate a price that should be a float with 2 decimal numbers, and it can be 1 number before the . or 9 digits. For example:
0.50
1.00
99999.99
999999999.00
I tried this:
v => (/\d{1,3}(?:[.,]\d{3})*(?:[.,]\d{2})/.test(v))
But doesn't work.
Sorry if my english is not very good. I appreciate the help!
To match 1-9 digits before the dot, and 2 decimal numbers:
^\d{1,9}\.\d{1,2}$
See a regex101 demo.
What do you want? Check the value for matching a number from 0 to 999999999 in the integer part and no more than 2 numbers after "."?
A template assuming that the entire string being checked from the beginning (^) to the end ($) consists of
mandatory initial part, which is either 0 or contains from 1 to 9 digits, and does not start with "0" ;
optional ending of "." and two digits:
^([1-9]\d{0,8}|0)(.\d{1,2})?$
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 2 years ago.
Improve this question
I want to write a regular expression which would look like for ex: H005-2007-652764
First char should start with `H`
5th and 10th char should be `-`
Remaining all should be digits
Your requirements are perhaps not totally complete, but if I assume that you only want six digit characters at the end, something like the regex /H\d{3}-\d{4}-\d{6}/ would work. You can see it working here or in the snippet below:
const text = 'nonsense nonsense lorem ipsum H005-2007-652764 and then more nonsense';
const regex = /H\d{3}-\d{4}-\d{6}/;
console.log(text.match(regex));
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I cannot understand the function used below that just splits a string on '.'.
Could you help me understand why it uses the extra replace statements?
function dotSplit (str) {
return str.replace(/\1/g, '\u0002LITERAL\\1LITERAL\u0002')
.replace(/\\\./g, '\u0001')
.split(/\./).map(function (part) {
return part.replace(/\1/g, '\\.')
.replace(/\2LITERAL\\1LITERAL\2/g, '\u0001')
})
}
Here, \1 means to match the character whose octal representation in Latin-1 encoding is 1. That character is SOH, or the start of heading character. What it does above is replace all occurrences of that with \u0002LITERAL\\1LITERAL\u0002, where \u2002 stands for the character STX(Start of text).
You can try it here:
https://regex101.com/r/n9LaJY/1
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am writing a code where I am receiving a number of exactly 11 or 13 digits in it. But, the problem is that it may contain some hyphens at random places.
Can anyone suggest a regular expression for this?
Sample inputs (assuming only 5 digits):
1. 12345
2. 1-234-5
3. 12-34-5
4. 123-45
5. 1-2-34-5
Try this code snippet. It may help you.
var str="123-45";
str.replace( /\D+/g, '');
Here,
\D - Find a non-digit character.
so, Code will replace non-digit with ''.
It would be significantly easier, and infinitely more readable to remove all dashes, and then count the remaining characters.
var str = "1-234-5";
var res = str.replace(/-/g, '').length;
if(res === 11 || res === 13) {
//do whatever
}
Have a try with:
^(?:-?\d){11}(?:-?\d-?\d)?$
or, if - can't be in first place:
^(?:\d-?){11}(?:\d-?\d)?$
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 8 years ago.
Improve this question
i want to set validation rule for company names which should start with character and it can contain numbers and special characters..
For example:
360 Angel - should not be allowed
Angel 360 - Allowed
Javascript regular expressions should help
Documentation:
http://www.javascriptkit.com/javatutors/redev3.shtml
Simple example:
var str = "360 Angel";
/^[a-zA-Z].*/.test(str); // return false