Regular expression for percentage - javascript

I have the following RegExp which verifies the value to be 100%
(^100([.]0{1,2})?)$|(^\\d{1,2}([.]\\d{1,2})?)$
I'm trying to get it to accept percentages without the leading zero, example
.5 rather than just 0.5.
If someone could help me to correct this expression so that it would accept both, I'd really appreciate it.

Here you should find your correct regular expression.

You can add ^([.]\d{1,2})?) as another possible string:
(^100([.]0{1,2})?)$|(^\d{1,2}([.]\d{1,2})?)$|(^([.]\d{1,2})?)$

Related

Get an example matched text from a regex pattern [duplicate]

Is there any way of generating random text which satisfies provided regular expression.
I am looking for a function which works like below
var reg = Some Regular Expression
var str = RandString(reg)
I have seen fairly good solutions in perl and ruby on github, but I think there are technical issues that make a complete solution impossible. For example, /[0-9]+/ has an infinite upper bound, which is not practical for selecting random numbers from.
Never seen it in JavaScript, but you could translate.
EDIT: After googling for a few seconds...
https://github.com/fent/randexp.js
if you know what the regular expression is, you can just generate random strings, then use a function that references the index of the letters and changes them as needed. Regex expressions vary widely, so it will be difficult to find one in particular that satisfies all possible regex.
Your question is pretty open so hopefully this steers you to the right solution. Get the current time (in seconds), MD5 it, check it against a REGEX, return the match.
Running Example: http://jsfiddle.net/MattLo/3gKrb/
Usage: RandString(/([A-Za-z])/ig); // expected to be a string
For JavaScript, the following modules can generate a random match to a regex:
pxeger
randexp.js
regexgen

javascript number regular expression

I've come up with this regular expression to validate a javascript number according to the specification:
(-|\+|)(\d+\.?\d*|\.\d+)([eE](-|\+|)\d+)?
As far as I can think of, these are valid numbers in js:
123,123.3, .3, -123, -.3, -.3e-2, -.3e+2, +.2e2... and so forth.
I've been trying to find a verified regular expression on the internet so that I could compare my solution but to no avail.
Could anyone tell me if my approach is correct or give me a better solution?
Link to test my solution
While using isNan is the correct way of checking numbers in JavaScript, you can also validate floating point numbers with [-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)? regex (taken from Regular-Expressions.info).
Consider using appropriate anchors though! (^ for string start, $ for string end).
Demo is available here.

Regular expression needed for 3 variants that all start the same and must be one of the three

I am trying to get the following into a js regex:
I need to include the following
gtm.click
gtm.linkClick
gtm.formSubmit
There are other things that start with gtm. so it cannot just be "gtm.*"
I don;t use regular expressions often enough. Any help on this appreciated. I was trying the following without luck:
gtm\.*click|gtm.formSubmit
What would be the right way to do this?
If you just want those then
gtm\.(click|linkClick|formSubmit)
Simply use:
gtm\.(click|linkClick|formSubmit)
Explanation here
The below regex will match anything which starts with gtm. and continues with at least one letter:
(gtm\.)[a-zA-Z]+
If you want to match numbers also use:
(gtm\.)[a-zA-Z0-9]+

Getting this regex expression to work in javascript

I have an html checkbox element with the following name:
type_config[selected_licenses][CC BY-NC-ND 3.0]
I would like to break this name apart as follows and returned as part of an array:
["type_config", "[selected_licenses]", "[CC BY-NC-ND 3.0]", "[selected_licenses][CC BY-NC-ND 3.0]"]
I thought I could do this by using a regular expression in javascript. Here is the expression that I am using:
matches = /([a-zA-Z0-9_]*)((\[[a-zA-Z0-9_\.\s]*\])+)*/.exec(element_name);
but this is the result I am getting in my matches variable:
["type_config[selected_licenses]", "type_config", "[selected_licenses]", "[selected_licenses]", index: 0, input: "type_config[selected_licenses][CC BY-ND 3.0]"]
I am half way there. What am I doing wrong in my regular expression? I guess I should also ask if it is possible to accomplish what I want with a regex?
Thanks.
The problem with this kind of goal is that there's no simple way to achieve this with regular expression, i.e. a simple match call. In short, even if you put a quantifier after a capturing group, the captured string will always be just one.
You'll have to rely on something more specific, like breaking the string with a repeated use of indexOf, or something like
name.split(/(?=\[)/);
Maybe you want to be sure that name is formally correct.
This is a very ugly problem. I don't know how repeatable this is, but I can do it:
Regex
^(\w+)(?<firstbracket>\[(?<secondbracket>[^]]*)\]\[(.*?)\])$
Replacement
["$1", "[$3]", "[$4]", "$2"]
Demo
http://regex101.com/r/eD9mH8

Javascript regex to match a person's height

I need a javascript regex pattern to match a person's height to check if the input is valid. Here are some sample input:
5' 9"
6'
5'8"
Any ideas?
If you want to make sure that no one mucks around with it, you could limit it to sensible ranges, eg: 3' to 7'11''
/^(3-7)'(?:\s*(?:1[01]|0-9)(''|"))?$/
I always thought that the "inches" mark was a double quote ("), compared to VonC's answer where he put it as two single quotes (''), so this regex takes both into consideration.
Maybe something like:
^(\d{1,5})\'((\s?)(-?)(\s?)([0-9]|(1[0-1]))\")?$
see: here
Something like:
\d'(?:\s*\d+'')?
The second part refers to optional part of the heigth.
Remove the + if you want only one digit.
\b\d'(?:\s*\d+'')?\b
can also be used to detect that pattern within a text (avoid detecting 1234'45 as an heigth for... a person?!)
You can test that regexp here for javascript.
Ok. Thanks for all your input. Wow, that was fast, Big time.
Anyway, I've tested all your regex and it seems Ruben's answer passed all my test input. Thanks a lot for that mate.
So here's the one that I need:
^(\d{1,5})\'((\s?)(-?)(\s?)([0-9]|(1[0-1]))\")?$
^\d'\s?(\d{1,2}")?$
Tested here: http://www.regular-expressions.info/javascriptexample.html

Categories

Resources