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

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

Related

Remove single quotes and last comma in jquery string using Regex [duplicate]

This question already has answers here:
JavaScript REGEX Match all and replace
(2 answers)
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I have a data
'abc','def','ghi',
I want to remove the single quote on all character and want to remove only the last comma, example like below
abc,def,ghi
How would i achieve that using regex for javascript?
I tried using this regex
.replace(^\'|,\s*$,"");
But seems like it is only removing the first quote as shown below
abc','def','ghi',
I am not very good in regex, i appreciate any help that i can get. Thanks
try this:
.replace(/\'|,$/g, "");
the ^ at the beggining made the regexp to only match the quote at the beggining of the string, also you have to add the g to keep looking after the first match
There is an easy way, use the $ operator
.replace(/'|,$/g, '')
Can you please check replace(/'|(,)$/g," ") and it should work.

invalid Regex group [duplicate]

This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 4 years ago.
I'm trying to create the following regex using Javascript.
(?<!\\)(?:\\{2})*\\(?!\\)([5-9]|[1-9]\d)
However, by doing this it gives me invalid group error in the console.
regExp = new RegExp("(?<!\\)(?:\\{2})*\\(?!\\)([5-9]|[1-9]\d)", "gi");
I don't understand where the problem comes from exactly. I appreciate the help.
Thank you
EDIT: After some research I found that Javascript does not support lookbehinds.
So the error comes from (?<!\\).
Refer this newly asked question to find an alternative way to do the same job.
How to check for odd numbers of backslashes in a regex using Javascript?
If your expression isn't dynamic, just use a literal:
var regExp = /(?<!\\)(?:\\{2})*\\(?!\\)([5-9]|[1-9]\d)/gi;
The problem is that your escape sequences \\ inside the string end up rendering \ characters inside the regEx, which in turn end up escaping brackets they shouldn't, resulting in unterminated groups.

Matching the first line of content in RegEx [duplicate]

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.

JavaScript special characters misunderstanding [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 8 years ago.
In this javascript code I have this row:
var regex = /\s+/gi;
Any idea what is the maning of this:
/\s+/gi
Thank you in advance.
Here is a must read for same https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
Regular expressions are patterns used to match character combinations in strings. In JavaScript, regular expressions are also objects. These patterns are used with the exec and test methods of RegExp, and with the match, replace, search, and split methods of String. This chapter describes JavaScript regular expressions.
Here, each contiguous string of space characters is being replaced with the empty string
For more details refer :-
Is there a difference between /\s/g and /\s+/g?

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