This question already has answers here:
Numeric validation with RegExp to prevent invalid user input
(3 answers)
Closed 7 years ago.
I need a regex for any number OR float.
I have used this but doesn't work:
/(^[0-9]+*[.][0-9]+)$|^[\d+]$/
Why ?
Try this regex:
/^[+-]*[0-9]+[.][0-9]+|[+-]*[0-9]+$/g
You can use http://www.regexr.com/ to test and create regexes
Try this one:
^[-+]?[0-9]*\.?[0-9]+$
In case this might help you. This online Regex Tools is actually very helpful:
http://www.regexr.com/
You have a few issues with your regex:
You cannot have a quantifier after + so change +* to just + (1 or more matches)
Move your start identifier ^ outside of the group, for consistency
hmmm, you seem to have edited your regex: but [^\d+] was looking for NOT a number or + symbol.
One possible solution would be as follows:
^([0-9]+[.][0-9]+)$|^\d+$
Here is a working example
For more example, see here
You expression doesn't work because it contains errors.
This is the website I use for RegEx testing - It is good to test them on a website like this that gives you feedback
You have the beginning inside a capturing group, but the end outside - (^...)$
You also have double operators - +* - Use only one
And the or not plus sign and digit is not needed - ?[^\d+]
I believe this expression will do what you want: ^-?\d+(\.\d+)?$
Related
This question already has answers here:
How to detect exact length in regex
(8 answers)
Closed 4 years ago.
So I am trying to use a regular expression to check against strings but it doesn't seem to be working properly.
Basically I want it to match a alpha-numeric string that is exactly 3 characters long. The expression I am using below does not seem to be working for this:
const msg = message.content;
const regex = /[A-Za-z0-9]{3}/g;
if (msg.match(regex)) {
// Do something
}
Am I doing something wrong? Any help would be appreciated. Thanks in advance.
You need to add ^ and $ for the start-of-string anchor and end-of-string anchor, respectively - otherwise, for example, for #123, the 123 will match, and it will pass the regex. You also might consider using the i flag rather than repeat A-Za-z, and you can use \d instead of 0-9.
It looks like you just want to check whether the string passes the regex's test or not, in which case .test (evaluates to a boolean) might be a bit more appropriate than .match. Also, either way, there's no need for the global flag if you're just checking whether a string passes a regex:
const regex = /^[a-z\d]{3}$/i;
if (regex.test(msg)) {
// do something
}
This question already has answers here:
Regular Expression to find a string included between two characters while EXCLUDING the delimiters
(13 answers)
Closed 7 years ago.
An API I use returns this text:
<http://192.168.1.10:8080/longUrl>; rel="recording-session",
<http://192.168.1.10:8080/realLongUrl>; rel="h264-session-sdp",
<http://192.168.1.10:8080/realLongDifferentUrl>; rel="h264-session-sdp",
<rtp://239.1.1.18:5006>; rel="destination-high",
<rtp://239.1.1.17:5006>; rel="destination-low"
I'm trying to retrieve the first URL that is followed by ; rel="h264-session-sdp.
So in this case that would be: http://192.168.1.10:8080/realLongUrl
I've been fiddling around trying to modify examples found here on SO, but just can's seem to get it right.
try this one /([^<]+)(?:>; rel\=\"h264\-session\-sdp\")/
Selects the text inbetween the greater then and less then characters:
?<=\<)(.*?)(?=>)
https://regex101.com/r/oS5sX6/1
And if you wanted to select the urls on multiple lines, add the g and m modifiers:
/(?<=\<)(.*?)(?=>)/gm
https://regex101.com/r/oS5sX6/2
Try this:
/(?<=\<)(.*?)(?=\>)>; rel="h264-session-sdp"/
This question already has answers here:
How do I replace an asterisk in Javascript using replace()?
(6 answers)
Closed 7 years ago.
I am trying to replace the same string *a*a consistently with *a.
Tried many variations of something like this, but none really worked:
s = s.replace( /\b*a*a\b/g, "*a");
So far running this leads to all xzy*a being replaced with xyz
* is a special regex character. If you want to match only an actual asterisk, then you have to escape it like this:
s = s.replace( /\*a\*a/g, "*a");
Working demo: http://jsfiddle.net/jfriend00/gvgshwyz/
An asterisk is a special regex character.
You just have to escape it like this: \*a in place of *a
This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 4 years ago.
I'm trying to further my understanding of regular expressions in JavaScript.
So I have a form that allows a user to provide any string of characters. I'd like to take that string and remove any character that isn't a number, parenthesis, +, -, *, /, or ^. I'm trying to write a negating regex to grab anything that isn't valid and remove it. So far the code concerning this issue looks like this:
var pattern = /[^-\+\(\)\*\/\^0-9]*/g;
function validate (form) {
var string = form.input.value;
string.replace(pattern, '');
alert(string);
};
This regex works as intended on http://www.infobyip.com/regularexpressioncalculator.php regex tester, but always alerts with the exact string I supply without making any changes in the calculator. Any advice or pointers would be greatly appreciated.
The replace method doesn't modify the string. It creates a new string with the result of the replacement and returns it. You need to assign the result of the replacement back to the variable:
string = string.replace(pattern, '');
This question already has answers here:
Regex phone number to accept + [duplicate]
(4 answers)
Closed 3 years ago.
I want to validate a phone number this format +905555555555.
How can I write a regex expression to test for this?
one "+" and 12 numbers. hint: escape the "+".
^\+(90)\([2-5]{1}\)[0-9]{9}
or not starts with +;
\+(90)\([2-5]{1}\)[0-9]{9}
<script type="text/javascript">
var test = "+905555555555";
var re = new RegExp(/^\+[0-9]{12}$/gi);
if(re.test(test))
{
alert("Number is valid");
} else {
alert("Number is not valid");
}
</script>
Check out this site for testing out your Regular Expressions: http://www.regextester.com/index2.html
And a good starting point to learn is here:
http://www.regular-expressions.info/
if you need it to start with a "+" and a "9" and 11 digits:
^[+]9\d{11}$
I recommend that you understand how regEx work, take a look at this usefull tester:
http://www.sweeting.org/mark/html/revalid.php
At the bottom they explain what each operator means.
Also there are all sort of examples at the internet.
EDIT: after reading the OP's comments, in order for the number to start with "+90" and then have 10 digits, you can use the following expression:
^[+]90\d{10}$
To cover your additional specifications use this:
^\+90\([2-5]\)\d{9}$
You definitely need the anchors ^ and $ to ensure that there is nothing ahead or after your string. e.g. if you don't use the $ the number can be longer as your specified 12 numbers.
You can see it online here on Regexr
If it's exactly that format then you could use something like: /^\+\d{12}\z/
If you'd like to allow some spaces/dashes/periods in it but still keep it at 12 numbers:
/^\+(?:\d[ .-]?){12}\z/
Then you could remove those other chars with:
s/[ .-]+//g (Perl/etc notation)
Adjusted a bit to answer question for a 12 digit regex.
// +905555555555 is +90 (555) 555-5555
let num = '+905555555555';
/^\+90\d{10}$/.test(num);