Matching the first line of content in RegEx [duplicate] - javascript

This question already has answers here:
Using regular expressions to validate a numeric range
(11 answers)
Closed 8 months ago.
I want to match numbers between 0 and 799 ONLY,if it doesn't have a comma in them.
$660
http://stackoverflow.com/questions/6560030/what-regex-can-i-use-tovalidate-anumber-between-0-and-255
I've tried using this RegEx. --> \b(0*(?:[0-9]?[0-9]?[0-9]?|100))\b and it works very well.
(If the number is between 0 and 999)
Need help with changing my regex:
I need it to work in JavaScript.
I'd like to validate the number in the first row using regex after the $ (I only need
It,if It's between 0 and 799)
If it has a comma in it then it should be ignored( like numbers 799+)
I don't want it to accept numbers with comma in them,because my current regex thinks it's valid.
(Or at least the 6,245 should be equal to 6245 so my regex can ignore it.)

To rephrase your question, numbers must not have a comma before them, or after:
(?<!,)\b[1-7]?\d?\d\b(?!,)
Try it online.
If you can't use look behinds, eg if you use JavaScript, you'll have to consume the non-comma and capture the target instead:
(^|[^,])(\b[1-7]?\d?\d\b(?!,))
The number is in group 1.
Try it online.

Related

How to check the sting is alphanumeric or not in peggyjs [duplicate]

This question already has answers here:
RegEx for Javascript to allow only alphanumeric
(22 answers)
Closed 1 year ago.
I want to check the given string is alphanumeric or not. i.e. the expected output is as follows
123 should retun false
abc should retun false
a123 should retun true
1a23 should retun true
I tried with the ^[a-zA-Z0-9]*$ regex. It is not working as expected. Can anyone suggest the working peggyjs regex? Thanks.
You can assert not only digits, and match at least a single digit restricticting to match to only a-z or a digit.
Using a case insensitive match:
^(?!\d+$)[a-z]*\d[a-z\d]*$
Regex demo
If you know the order (letters then numbers for example) you can do .*[a-zA-Z].*[0-9]
But I assume you can't make such assumptions so I would use the slightly more complex ^(?=.*[a-zA-Z])(?=.*[0-9]).* which means "a letter somewhere later, and also a number somewhere later".
PS : you can replace all [0-9] by \d if you like.
Edit : that's only assuming you don't get other kinds of characters, use Alireza's regex instead if you need to.

Regex failing for lower case, upper case, numbers and min 8 characters [duplicate]

This question already has answers here:
Regex for password must contain at least eight characters, at least one number and both lower and uppercase letters and special characters
(42 answers)
Closed 3 years ago.
I've looked and found so much information for regex. It's super well documented, but I'm clearly being an idiot, or have looked at this issue for too long!
The pattern I need to match is any number of upper case, lower case and numbers, with at least 8 characters. I don't want to accept anything else, such as non-alphanumeric characters (_ *^& etc)
My effort is
^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])\S{8,}$
Sadly, when I use https://regex101.com/ this does not match any of the following
aaaaaaaa
AAAAAAAA
00000000
asdfFDSA167
#fFaf9374A
12345678
123456NBh
2 of those are valid but I don't see why I'm having issues
The end goal is to use this in the pattern attribute for input (HTML 5 input pattern="" />`)
I believe this is what you're looking for: ^[a-zA-Z0-9]{8,}$
Or maybe this: ^[a-z]{8,}|[A-Z]{8,}|[0-9]{8,}$
The first one will match any combination of letters/numbers and the second one will only match sequences of like-characters. It's hard to tell exactly what you're looking for given the question.
EDIT:
I made a mistake but fixed it
Try this :
^(?=.*[A-Z])(?=.*[a-z])(?=.*[\d])([A-Za-z\d]{8,})$
DEMO

How to validate a Phone Number With `-` and spaces [duplicate]

This question already has answers here:
How to validate phone numbers using regex
(43 answers)
Closed 9 years ago.
I want to validate the phone number Like 1-12 -123 using regex and I have tried like
/^[0-9 -]+$/
and
/^[0-9,\- ]+$/
Iam using it like
/^[0-9 -]+$/.test(value)
And it is accepting 0-9 numbers and - also but not accepting the spaces.I have tried in many ways but did'nt got any solution.Can anyone suggest me.Thanks in Advance
you can use [ -0-9]+ (leave a space after the first square bracket) to allow spaces, dashes and numbers
For javascript :
var phoneNumber = "1-12 -123";
var test = /^[0-9 -]+$/.test(phoneNumber);
alert(test); // Here, return true
If you code in PHP, this code will match your phone number.
<?php
$phoneNumber = '1-12 -123';
if(preg_match('/^[0-9 -]+$/', $phoneNumber)) {
echo "Phone number is valid";
}
Edit: Ok, use the right tag next time.
What I'd recommend is trimming out the spaces and the - from the field, and then validate the digits only.
Because I doubt
-----0-4 023 - 331 34 124
00000 -
-
etc
should be considered a valid phone-numbers?
And if you then try to make a phone-number regex to take that into considerations you'll either not succeed, or run into an expression that's so complex that it's not maintainable.
So instead trim your input/format your input into a format you control, which you then easily can validate and use that.
If you really want to go a Regular Expression route, I'd think something like
^[1-9]([ \-0-9][0-9]+)+$
is what you're after.
(if the number must not start with 0, otherwise replace the first [1-9] with [0-9])
Also change the final + into {0;y} for how many blocks of numbers you allow etc if there's a limit/focus on how many 'blocks' of numbers you allow.
Regular expressions can quickly become very complex.
this one works [0-9 -]+, this doesn't work [0-9- ]+ (order matters)
For spaces you need to put a \s in your alphabet.
/^[0-9\s-]+$/

regex to allow only numbers and single dot in jquery [duplicate]

This question already has answers here:
Regex allow digits and a single dot
(5 answers)
Closed 9 years ago.
Regex to allow only numbers and single dot when entering number to textbox in jquery.
Please suggest any regex to allow only numbers and single dot in textbox.
I have tried the following code.
$("#amountId").val().replace( /[^0-9]+/g, '')
[-+]?([0-9]*\.[0-9]+|[0-9]+).
See Matching Floating Point Numbers With Regex
This will find something like this 1234.5678
/\d+\.\d+/
This regex should do the trick for you, \d+\.$, and here is a Rubular to prove it. You could even consider prefacing the string with the ^ so that nothing can come before the digits too, like this, ^\d+\.$.
I would try using the following:
\d+\.\d?{2}
http://rubular.com/r/a83BWznDuy

Javascript regex to extract the string before the last backslash [duplicate]

This question already has answers here:
Regex for everything before last forward or backward slash
(3 answers)
Closed 9 years ago.
I am dealing with timezone's in Javascript and I need a regex that will extract everything, but the timezone name from it. For example, I have the timezone America/Argentina/Buenos_Aires. I want to extract the America/Argentina part with a regex. Currently I have this regex: tz.match(/.*?(?=\/|$)/i)[0] which extracts everything to the first backslash which works for most timezones (America/Los_Angeles), but not for all of them. How could I edit that regex so that it gets the string before the last value?
I'd personally suggest avoiding regular expressions for something like this, when simple string functions/methods would suffice admirably:
var stringVariable = 'America/Argentina/Buenos_Aires',
text = stringVariable.substring(0, stringVariable.lastIndexOf('/'));

Categories

Resources