Javascript question regarding the use of regex - javascript

I need to find a regex expression for a number 1 to 9, followed by trailing zeroes, followed by the end number 1 to 9. As like in a minesweeper game for clearing zeroes.
How do I match the part of an array where like i have 10009 or 2003 ? ,1to9 then zeroes, then 1to9?
How would I write it?
does this work?
updated: how do I ask this regex or another regex? the one i have below or a (trailing zeroes and 1-9)
(^[1-9][0+][1-9]$)

[1-9][0]+[1-9]
Move the + outside of the square brackets. Otherwise, it will match the literal character.
const regex = new RegExp("[1-9][0]+[1-9]")
function test(testCase){
console.log(regex.test(testCase))
}
test("10009")
test("2003")
To make the first digit optional, you can do:
[1-9]?[0]+[1-9]
const regex = new RegExp("[1-9]?[0]+[1-9]")
function test(testCase){
console.log(regex.test(testCase))
}
test("0009")
test("2003")

When you say [0+] then you are saying that select any of either 0 or +
you want is quantifier 0+ which means 0 one or more times
You can use ^[1-9]0+[1-9]$
const regex = /^[1-9]0+[1-9]$/;
function test(str) {
return regex.test(str);
}
console.log(test("10009"));
console.log(test("2003"));

Related

Regex validating string of numbers

I am trying create regex witch will start with some number like 230, 420, 7456. Then could be some number in interval 1-9 but the final length must be 9.
For example 230888333 or 745623777
I create this:
([(230|420|7456)[0-9]{1,}]{9})
But it is not correct. Any idea how to make this correct?
Thaks.
The pattern that you tried is not anchored, and the current notation uses a character class [...] instead of a grouping (the ]{9} part at the end repeats 9 times a ] char)
If you use C# then use [0-9] to match a digit 0-9.
You can either assert 9 digits in total:
^(?=[0-9]{9}$)(?:230|420|7456)[0-9]+$
Regex demo
Or write an alternation for different leading lengths of digits:
^(?:(?:230|420)[0-9]{6}|7456[0-9]{5})$
Regex demo
You can simply add a check for length first and then simple regex
function checkString(str){
return str.length === 9 && /^(?:230|420|7456)\d+$/.test(str)
}
console.log(checkString("230888333"))
console.log(checkString("745623777"))
console.log(checkString("123"))
console.log(checkString("230888333123"))

regular expression for validating fields

Validations i'm trying :
regular expression of : 9999.99 and no spaces (0009.99 it should convert 9.99
Edit :
var regex = '(?!0)\d+(?:\.\d+)?$';
function getValue() {
// passing value 0009.99 and 0009.00 and 100
return document.getElementById("myinput").value;
}
function test() {
alert(regex.test(getValue()));
}
function match() {
alert(getValue().match(regex));
}
Your first and second seems to Work just fine, the third can be achieved with the following regex:
/(?!0)\d+\.\d+$/
It starts by looking forward for zeros (skipping them), then it matches any number of digits followed by a dot and more digits. If you want the digits to be optional you can replace the plus '+' with a star '*'.
Edit:
If you want to allow integers, you can use this regex:
/(?!0)\d+(?:\.\d+)?$/
That makes the dot and the digits after that optional.
BTW: Your jsfiddle does not help in answering.
Edit2:
To create a Regex using quotes you must use the following syntax:
var regex = new RegExp('(?!0)\d+(?:\.\d+)?$');
Edit3:
I forgot to mention, you need to double escape backslashes, it should be:
var regex = new RegExp('(?!0)\\d+(?:\\.\\d+)?$');
Now it should Work directly in your code.

Find number that follows certain string which includes both letters and punctuation

I am trying to find a way to extract the numbers that occur after abc/ immediately succeeding the / and before any further letters, numbers or punctuation.
E.g:
abc/134567/something should return 1234567
abc/1234567?foo=bar should still only return 1234567
blah/1234/abc/678 should only return 678 as I'm looking only for the number that succeeds abc/
I'm aware there are two options: regex or substring match.
In order to perform the substring match I need the index point but I'm dubious about merely doing an indexOf("abc/") as it only returns the index of the first letter - a - which could be present elsewhere in the string.
With regex I have struggled as I find that searching for a mixture of the letters and the slashes seems to cause it to return null.
So what's the best way?
You can use this regexpression :
var rgx = new RegExp("abc\/([0-9]+)","gi");
Then :
var m = rgx.exec("abc/1234567?foo=bar");
console.log(m[0]);
edited after comments
You could use a regular expression and seach for abc/ and following digits.
var array = ['abc/134567/something', 'abc/1234567?foo=bar', 'blah/1234/abc/678'];
console.log(array.map(s => s.match(/abc\/(\d+)/)[1]));
We accept string that has abc/, after it an integer number, that is taken as a matched group and either the end of string or some non-digit symbol after it.
abc\/(\d+)(?:$|\D)
test
You'll use in Javascript for matched group extraction:
var myRegexp = /abc\/(\d+)(?:$|\D)/g;
var match = myRegexp.exec(inputString);
var result=match[1]; // the number after abc/
In another regex engine than that of JavaScript, lookahead and lookbehind could be used. But in JS lookbehinds are forbidden. :-(. So we have to use this, a bit more complicated, way.
Are you after something like this:
^(.*\/)(\d+)(.*)
Where the second group will give you the digits after the slash.
Look at the regex here

Removes all after the first block of numbers

I'm trying to write code that removes all after the first block of numbers and text.Do you have any idea how to do this.
string = '009EPMT18$MBS'
the expected result
string = '009EPMT'
You'll need regex to do that. It's a string analysis syntax common in many languages. There are many regular expressions which would do what you want, here's one:
var myRegex = /^[0-9]+[a-zA-Z]+/;
^ means that the search must begin at the start of the string.
[0-9] means that right after the beginning, there must be characters in the 0 to 9 range.
+ means there must be one or more of the previous condition, meaning there must be one or more digits.
[a-zA-Z] means there must be any character in the range a to z or A to Z. This won't include accented characters and letters from other alphabets though.
Calling .exec(string) on a regex returns an array of found strings in the passed string.
You were on the right track, the letters were just missing from your pattern:
var s = '009EPMT18$MBS';
var result;
var m = s.match(/^\d+[A-Z]+/); // first numbers and uppercase text
if (m) result = m[0]; // result = "009EPMT"
Regex explanation: beginning of string ^ followed by 1 or more digits \d+ followed by 1 or more letters from A to Z [A-Z]+. Note that lowercase characters will not match.

Javascript regex to return number following a hyphen and a space

I am trying to write a regular expression to extract the number on the right hand side of a string split by a hyphen.
For 1 - 15 I want to return 15.
For 0 - 8 I want to return 8.
Bear in mind I don't want to include the white-space between the hyphen and the number.
I've tried [^-\s]\d, but this isn't right.
You can also use a non capturing group:
(?:\d+)\s-\s(\d+)
This one will look for:
(?:\d+) //a group of one or more numbers
\s //a space
- //a hyphen
\s //a space
(\d+) //a second group of one or more numbers.
The ?: tells it to throw the first match out and so it only returns the second match (after the hyphen).
this pattern should also work (\d+)\D*$ Demo
You can use:
str = '1 - 15';
var num = str.replace(/^[^-]*-\s*(\d+).*$/, '$1');
//=> 15
normaly it should work.
use dollar at the end
[^-\s]\d$

Categories