Javascript regex to extract count at end of file - javascript

I am fairly new to regex and am in need of some help.
The end goal is to capture the final sequence of numbers before the period in a file name. For example, I want to extract the '04' from 'test687.09ew0_d04.jpg'. Another example would be the '787' from '039lksdkl3200dj787.jpg'.
This is my regex so far:
/([0-9]+)(?:\.[\s\S]+)$/
My understanding is that the ([0-9]+) matches any number of digits 1 through 9.
The (?:\.[\s\S]+) specifies that the digits must be followed by a dot and any number of characters, the ?: marking that it should not be captured, but used to match.
The $ at the end is meant to only match if the requirements are at the end of the string and nowhere else.
However, it is not working. The non-capture groups are returned in my matches, and the regex is not requiring that the match be at the end of the string.

You could use a negated character class such as [^.] to match all the non-. characters at the end of the file name, [^.]+$.
Then match the literal . character, \., and capture the previous digits, (\d+).
(\d+)\.[^.]+$
The expression above would capture 04 in the string test687.09ew0_d04.jpg - example here.

I want to extract the '04' from 'test687.09ew0_d04.jpg'.
The solution using String.prototype.match() function:
var getLastDigits = function(str){
var m = str.match(/(\d+)\.\w+$/);
return m[1];
};
console.log(getLastDigits('test687.09ew0_d04.jpg'));
console.log(getLastDigits('039lksdkl3200dj787.jpg'));

var regex = /(\d+)\.[^\.]*$/;
function check(){
var str = prompt("Filename example: ");
var m = str.match(regex);
if(m && m[1])
alert("Here you go: " + m[1]);
else
alert("Nothing matched!");
}
<button onclick="check()">TRY</button>

Related

Javascript match a string which start with a specific char from the set of chars and end with same char using Regular Expression

I need to achieve a particular task in javascript, in which my goal is to match the string which starts with a char from a specific set of characters like vowel and ends with the same character where the length of the string is greater than three.
so far I have done the following code that starts and ends with the same character but doesn't know how to specify it that the first char is from the specific set of character:
function regexVar() {
var re = /(.).*\1/
return re;
}
console.log("obcdo".test(s));
let suppose the specific set of chars is the vowel
(a, e, i, o, u)
in this case:
abcd ----> false
obcdo ----> true
ixyz ----> false
You need to use a character set to ensure the captured character is one of the ones you want, backreference the first captured group at the end of the pattern, not the third group (your pattern doesn't have 3 capture groups), use ^ and $ to anchor the pattern to the start and end of the string, and repeat with {2,} rather than * to make sure the whole string is at least 4 characters long:
/^([aeiou]).+\1$/
const re = /^([aeiou]).{2,}\1$/
console.log(
re.test('abcd'),
re.test('obcdo'),
re.test('ixyz')
);
You can use this pattern
/^([aeiou]).+\1$/i
^ - Start of string
([aeiou]) - Matches a,e,i,o,u any one of that. (group 1)
.+ - Match anything except new line.
\1 - Match group 1
$ - End of string
let startAndEnd = (str) =>{
return /^([aeiou]).+\1$/i.test(str)
}
console.log(startAndEnd(`ixyz`))
console.log(startAndEnd(`abcd`))
console.log(startAndEnd(`obcdo`))
If we are taking the set of vowels then,the regular expression for words beginning and ending with the same vowel is:
var re = /(\ba(\w+)a\b|\be(\w+)e\b|\bi(\w+)i\b|\bo(\w+)o\b|\bu(\w+)u\b)/g;
function regCheck(string){
let re = new RegExp(/^(a|e|i|o|u).*\1$/g);
return re.test(string);
}
regCheck('aewxyzae')

regex match not outputting the adjacent matches javascript

i was experimenting on regex in javascript. Then i came across an issue such that let consider string str = "+d+a+", I was trying to output those characters in the string which are surrounded by +, I used str.match(/\+[a-z]\+/ig), so here what I'm expecting is ["+d+","+a+"], but what i got is just ["+d+"], "+a+" is not showing in the output. Why?
.match(/.../g) returns all non-overlapping matches. Your regex requires a + sign on each side. Given your target string:
+d+a+
^^^
^^^
Your matches would have to overlap in the middle in order to return "+a+".
You can use look-ahead and a manual loop to find overlapping matches:
var str = "+d+a+";
var re = /(?=(\+[a-z]\+))/g;
var matches = [], m;
while (m = re.exec(str)) {
matches.push(m[1]);
re.lastIndex++;
}
console.log(matches);
With regex, when a character gets consumed with a match, then it won't count for the next match.
For example, a regex like /aba/g wouldn't find 2 aba's in a string like "ababa".
Because the second "a" was already consumed.
However, that can be overcome by using a positive lookahead (?=...).
Because lookaheads just check what's behind without actually consuming it.
So a regex like /(ab)(?=(a))/g would return 2 capture groups with 'ab' and 'a' for each 'aba'.
But in this case it just needs to be followed by 1 fixed character '+'.
So it can be simplified, because you don't really need capture groups for this one.
Example snippet:
var str = "+a+b+c+";
var matches = str.match(/\+[a-z]+(?=\+)/g).map(function(m){return m + '+'});
console.log(matches);

Javascript: Remove trailing chars from string if they are non-numeric

I am passing codes to an API. These codes are alphanumeric, like this one: M84.534D
I just found out that the API does not use the trailing letters. In other words, the API is expecting M84.534, no letter D at the end.
The problem I am having is that the format is not the same for the codes.
I may have M84.534DAC, or M84.534.
What I need to accomplish before sending the code is to remove any non-numeric characters from the end of the code, so in the examples:
M84.534D -> I need to pass M84.534
M84.534DAC -> I also need to pass M84.534
Is there any function or regex that will do that?
Thank you in advance to all.
You can use the regex below. It will remove anything from the end of the string that is not a number
let code = 'M84.534DAC'
console.log(code.replace(/[^0-9]+?$/, ""));
[^0-9] matches anything that is not a numer
+? Will match between 1 and unlimited times
$ Will match the end of the string
So linked together, it will match any non numbers at the end of the string, and replace them with nothing.
You could use the following expression:
\D*$
As in:
var somestring = "M84.534D".replace(/\D*$/, '');
console.log(somestring);
Explanation:
\D stands for not \d, the star * means zero or more times (greedily) and the $ anchors the expression to the end of the string.
Given your limited data sample, this simple regular expression does the trick. You just replace the match with an empty string.
I've used document.write just so we can see the results. You use this whatever way you want.
var testData = [
'M84.534D',
'M84.534DAC'
]
regex = /\D+$/
testData.forEach((item) => {
var cleanValue = item.replace(regex, '')
document.write(cleanValue + '<br>')
})
RegEx breakdown:
\D = Anything that's not a digit
+ = One or more occurrences
$ = End of line/input

How to use RegEx to ignore the first period and match all subsequent periods?

How to use RegEx to ignore the first period and match all subsequent periods?
For example:
1.23 (no match)
1.23.45 (matches the second period)
1.23.45.56 (matches the second and third periods)
I am trying to limit users from entering invalid numbers. So I will be using this RegEx to replace matches with empty strings.
I currently have /[^.0-9]+/ but it is not enough to disallow . after an (optional) initial .
Constrain the number between the start ^ and end anchor $, then specify the number pattern you require. Such as:
/^\d+\.?\d+?$/
Which allows 1 or more numbers, followed by an optional period, then optional numbers.
I suggest using a regex that will match 1+ digits, a period, and then any number of digits and periods capturing these 2 parts into separate groups. Then, inside a replace callback method, remove all periods with an additional replace:
var ss = ['1.23', '1.23.45', '1.23.45.56'];
var rx = /^(\d+\.)([\d.]*)$/;
for (var s of ss) {
var res = s.replace(rx, function($0,$1,$2) {
return $1+$2.replace(/\./g, '');
});
console.log(s, "=>", res);
}
Pattern details:
^ - start of string
(\d+\.) - Group 1 matching 1+ digits and a literal .
([\d.]*) - zero or more chars other than digits and a literal dot
$ - end of string.

Why does't regex match all numbers instead of numbers just at the end of the string?

I was just looking for a regex that would watch the last numerical (\d or [0-9]) in a given string , strings like:
var str = "7-Dec-1985"
var str = "#scrollto-section-4"
Of-course I found an answer in the following thread on SO HERE
I am using a regex like the following:
str.match(/\d+$/)
Works fine, no issues, now I used the following tool to analysis the regex HERE,
\d+ //matches greedy 0 to as many
$ - specifies that the search should start at the end of the string
But why does that above regex in the below example:
var str = "7-Dec-1985"
Match only 1985 why not 71985 ?
Because $ means "end of input" (or "end of line or end of input" if you specify the m flag), and \d+ means a contiguous series of digits (not digits mixed with other things). So \d+$ means "a contiguous series of digits right before the end."
If you want to match anywhere, remove the $. Additionally, if you want to match more than once, you'll need a g ("global") flag on it.
Examples -- your original:
var str = "7-Dec-1985";
document.body.innerHTML = JSON.stringify(str.match(/\d+$/));
Without the $, but no g:
var str = "7-Dec-1985";
document.body.innerHTML = JSON.stringify(str.match(/\d+/));
Without the $ and with g:
var str = "7-Dec-1985";
document.body.innerHTML = JSON.stringify(str.match(/\d+/g));
Sorry, but $ doesn't means start search at end of string.
Your regex \d+$ means match the number at end of string.
To match any number use \d+ like this.
Because there is -Dec- between 7 and 1985 which isn't digit. Also $ means end of line. So Your pattern just matches that number which is end of string (continuously).

Categories

Resources