I want a regular expression to get only numbers from a string.I want to ignore the number preceding with a character.
Example : "(a/(b1/8))*100
Here I dont want to fetch b1.I want to get only the numbers like 8,100 etc
You can use a word boundary, though that would not match after underscores:
\b\d+
(?<![a-zA-Z])\d+ should work
You can use a regular expression to find both numbers with and without a leading character, and only keep the ones without:
var str = "(a/(b1/8))*100";
var nums = [], s;
var re = /([a-z]?)(\d+)/g;
while (s = re.exec(str)) {
if (!s[1].length) nums.push(s[2]);
}
alert(nums);
Output:
8, 100
Demo: http://jsfiddle.net/Guffa/23BnQ/
for only number
^(\d ? \d* : (\-?\d+))\d*(\.?\d+:\d*) $
this will accept any numeric value include -1.4 , 1.3 , 100 , -100
i checked it for my custom numeric validation attribute in asp net
Related
I learn right now regular expression and need to know, how I can remove all except 10 numbers or maximum 10 number, I tried to create RegExp like this
var value = value.replace(/[^\d]/g, '')
You can use regex's {0,10} range of times to specify the length of the number.
My example will produce to matches,
[
"1348737734",
"8775"
]
It will match first number with the length of 10, and the rest of the number.
const str = 'asb13487377348775nvnn';
const result = str.match(/(\d{1,10})/g);
console.log(result);
I want to remove some text from string or integer using javascipt or jquery..
I have string "flow-[s/c]", "flow-max[s/c]","flow-min[s/c]", "Usage-[s/c]", "temperature"
And I want for each :
"flow", "flow-max","flow-min", "Usage", "temperature"
As you can see. I want to remove all the data after - found expect flow-max and flow-min
What I am doing :
var legendName = $(textElement).text().toLowerCase().replace(" ", "-");
Taking the legend Name example : "flow-[s/c]", "flow-max[s/c]"
var displayVal = legendName.split('-')[0];
remove all the data after - found
But I am not able to add condition for flow-max because at this case I will be having two - and two place like flow-min-[s/c]
var displayVal = $(textElement).text().replace(/\-?\[s\/c\]/, "");
The code /\-?\[s\/c\]/ is a regular expression, where:
/ at the start and end are the delimiters of the expression.
\ is an escape character, indicating that the following character should be taken literally (in our example we need it in front of -, [, / and ] because those are control character for regular expressions).
? means the previous character is optional.
So it replaces an optional dash (-) followed by the text [s/c], with an empty string.
Just use this simple regex /(max|min)\[.*?]|-\[.*?]/g. The regex is simple, if you see what it does separately.
The logic has been separated by | operator.
legendName = legendName.replace(/(max|min)\[.*?]|-\[.*?]/, "$1");
You can use lastoccur = legendName.lastIndexOf("-"); to find the last occur of "-" and then split your string.
Reference: http://www.w3schools.com/jsref/jsref_lastindexof.asp
I would like to create a javascript regex to test a string that would accept only characters from 0 to 9, a to z, A to Z and the followings chars: + * . for a total length between 1 and 10 characters.
I did this :
var reg = /[0-9A-Za-z\+\*\.]{1,12}/;
if(!reg.test($('#vat_id').val())) {
return false;
}
but this doesn't seem to work.
I tested it on http://www.regular-expressions.info/javascriptexample.html, I can input "$av" and it returns me "successful match"
where is the mistake ?
edit : the regex seems good now :
var reg = /^[0-9A-Za-z\+\*\.]{1,10}$/;
But why i can't make it work ?
see http://jsfiddle.net/R2WZT/
If you don't "anchor" the regular expression to indicate that matches should start at the beginning and end at the end of the test string, then that is taken to mean that you want to see if the pattern can be found anywhere in the string.
var reg = /^[0-9A-Za-z\+\*\.]{1,12}$/;
With ^ at the beginning and $ at the end, you indicate that the entire string must match the pattern; that is, that no characters appear in the string other than those that contribute to the match.
You're not setting it to match the start and end:
var reg = /^[0-9A-Za-z\+\*\.]{1,10}$/;
I need to count the number of email addresses that a user inputs. Those addresses could be separated by any of the following:
Comma followed by no space - a#example.com,c#example.com.com
Comma followed by any number of spaces (ie. someone might have a comma follow by 3 spaces or just 1) - a#example.com, c#example.com.com
Only white space - a#example.com c#example.com.com
New line
What's a good way to clean that up and reliably count the addresses?
I assume regular 'ole javascript could handle this, but for what it's worth I am using jQuery.
The simplest way is just replace all commas with whitespaces, then, split your string based on blank spaces. No need for conditions.
Here's a fiddle with an example on that.
var emails = input.split(/[\s,]+/);
FIDDLE
var str="YOUR_STR",
arr = [];
if( str.indexOf(',') >= 0 ) {
// if comma found then replace all extra space and split with comma
arr = str.replace(/\s/g,'').split(',');
} else {
// if comma not found
arr = str.split(' ');
}
var l = "a#example.com,c#example.com.com a#example.com, c#example.com.com a#example.com c#example.com.com";
var r = l.split(/ |, |,/);
Regular expressions make that fairly easy.
If there is change of more than one space, the regex can be changed a bit.
var r = l.split(/ +|, +|,/);
I am clueless about regular expressions, but I know that they're the right tool for what I'm trying to do here: I'm trying to extract a numerical value from a string like this one:
approval=not requested^assignment_group=12345678901234567890123456789012^category=Test^contact_type=phone^
Ideally, I'd extract the following from it: 12345678901234567890123456789012 None of the regexes I've tried have worked. How can I get the value I want from this string?
This will get all the numbers:
var myValue = /\d+/.exec(myString)
mystr.match(/assignment_group=([^\^]+)/)[1]; //=> "12345678901234567890123456789012"
This will find everything from the end of "assignment_group=" up to the next caret ^ symbol.
Try something like this:
/\^assignment_group=(\d*)\^/
This will get the number for assignment_group.
var str = 'approval=not requested^assignment_group=12345678901234567890123456789012^category=Test^contact_type=phone^',
regex = /\^assignment_group=(\d*)\^/,
matches = str.match(regex),
id = matches !== null ? matches[1] : '';
console.log(id);
If there is no chance of there being numbers anywhere but when you need them, you could just do:
\d+
the \d matches digits, and the + says "match any number of whatever this follows"