how to get length of substring in Regular Expression JavaScript [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
here is my regular expression
/^[a-zA-Z0-9-]{40}([a-zA-Z0-9-]{3}2[a-zA-Z0-9-]{12}){2,10}$/
There will be one condition: the length of every string after first 40 characters must be 16 characters. This 16 characters string will be non repeating and minimum of 2 times and maximum of 10 times. So i want to get the length of this sub-string which should be 16.
Here is input string:
string input="PR212D4EB2-6B25-4020-BD2A-941F69C0AA4102GEX2421262506027GEX2437345435522"
I want to match the length of "GEX2421262506027" and "GEX2437345435522". Like this, there will be 10 strings max. I want to validate if this length should be 16 characters.

try this
var pattern = /^[a-zA-Z0-9-]{40}([a-zA-Z0-9-]{3}2[a-zA-Z0-9-]{12}){2,10}$/;
var exp = text.match(pattern);
if (exp) {
alert(exp[0].length);
}

if you just want the last 16 characters:
var string = 'PR212D4EB2-6B25-4020-BD2A-941F69C0AA4102GEX2421262506027GEX2437345435522';
var matching = string.match(/^[a-zA-Z0-9-]{40}([a-zA-Z0-9-]{3}2[a-zA-Z0-9-]{12}){2,10}$/);
console.log(matching[1]);
console.log(matching[1].length);

Related

How to validate a number has a 2 decimal place precision with javaScript? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm trying to check if a number has a 2 decimal place precision. I know how to convert a number to a 2 decimal place precision like this:
var myNumber = 2.456;
var currentNumber = parseFloat(myNumber);
currentNumber = currentNumber.toFixed(2);
console.log(currentNumber);
so If I enter:
1 this will turn into 1.00
2.457 this will turn into 2.46
but how can I check if the result of my code has a 2 decimal place precision? Thanks a lot in advance!
Parse the number into a string, split by a period and check whether the second item's length is 2:
function isPrecise(num){
return String(num).split(".")[1]?.length == 2;
}
console.log(isPrecise(1.23))
console.log(isPrecise(1.2))
console.log(isPrecise(1))
console.log(isPrecise("1.23")) //works if its a string too
You can also use a regex:
function isPrecise(num){
return /\d+\.\d{2}/gm.test(String(num));
}
console.log(isPrecise(1.23))
console.log(isPrecise(1.2))
console.log(isPrecise("1.23")) //works if its a string too

JavaScript Regex Expression needed for split method [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm trying to remove characters after numbers and needs to include .pdf extension.
I cannot use index since the file name varies on dates and names.
ex: My File Name : Mar 1316 and A - sec.pdf
required output file name: Mar1316.pdf
can anyone help with the regex and split method?
If there can be only one sequence of numbers or you want until the last sequence you can use this regex: (.*[1-9]+)
.*: any character any number of time
[1-9]+: 1 number or more
const str = "Mar 1316 and A - sec.pdf";
const match = /.*[1-9]+/.exec(str);
if (match) {
// will equal 'Mar 1316.pdf'
const newStr = str.slice(0, match[0].length) + '.pdf';
}
I don't think you can do this with JS regex, simply because you need both lookbehind and lookahead in the same regex, which I believe you cannot do.
/\s+|(?<=\d).*(?=\.)/

Regex: 15 integers with a maximum of two decimals, excluding 0 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am trying to create a custom validation with regex but cant find the right one.
15 integers max, 2 decimals max. 0 is not allowed.
Im usign this regex at this moment: /^(?:\d{1,15}(?:[.,]\d{0,2})?|[.,]\d{1,2})$/
but that one stills allows a 0
Valid cases:
0,01
123,1
1234,50
123456789012345,20
invalid cases:
0
-1
13,421
123,223
1234567890123456
The below pattern matches a digit between 1 and 15 times, followed by an optional group comprising a comma then either one or two digits. The pattern matches the entire string (from start to end) due to the anchors. It begins with a negative lookahead to ensure the entire string is not just the character "0".
(?!^0$)^\d{1,15}(?:,\d{1,2})?$
It matches all valid cases and no invalid cases from your question.
Try it out here: https://regex101.com/r/kB8jXt/1

Remove leading + or 00 from number using regex [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I need to remove + or 00 from the beginning of a number in case they exist. So a number like +37253783478 would output 37253783478 and 0037253783478 would output 37253783478. What would the regex look like that matches this pattern?
EDIT: I've managed to remove the leading zeros using ^0+ but I can't figure out how to match both cases.
If I understand the requirement, the following will match both cases. Essentially, what you need to do is use the regex or operator |.
The following will remove all leading 0s
str.replace(/(^0+|^\+)/,'')
But if you just need to remove exactly two leading 0s, use this:
str.replace(/(^00|^\+)/,'')
And here it is in action on your examples:
let nums = ['+37253783478', '0037253783478', '0037253780478', '375378+0478'];
let replaced = nums.map(num => num.replace(/(^0+|^\+)/,''));
console.log(replaced);

Need a regular expression to match a substring n times [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
The community reviewed whether to reopen this question 7 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
The regex .*{n} will match any single character n times, but I need to match any single substring n times.
How do I do that?
To match the substring "foo" 3 times (for example "foofoofoo"), you could use the following:
(foo){3}
Or with a non-capturing group:
(?:foo){3}
As a side note, .*{n} wouldn't do what you think it does. The . will match any character, .* will match any number of any characters, and .*{n} will vary depending on the implementation but it will either be an invalid regex, be equivalent to .*, or match any number of any characters followed by the literal string '{n}'.
Try
(your sub string here){n}
e.g.
(cats){4}
try
/(\w+)\1{n-1}/
Example:
"abcbcbca".match(/(\w+)\1{2}/) if you wish to find bc being repeated 3 times.
If you are trying to match a given string repeated n times, just do (string){n}.

Categories

Resources