I have some text
I01:00:00:05
I01:00:00:04
I01:00:00:03
I01:00:00:02
I01:00:00:01
Is there a regex that will find each one?
I tried:
var locs = txt.match(/(([A-Z]\d\d\:\d\d\:\d\d\:\d\d)+)+/);
And it finds 5 copies of the first pattern that matches.
Is there a way to get an array with each of them in it?
Thanks
if you include the 'g' flag at the end it should work.
var locs = txt.match(/(([A-Z]\d\d\:\d\d\:\d\d\:\d\d)+)+/g);
You are simply missing the g flag on your regexp and it will find all the occurences that match the pattern.
Like so:
var locs = txt.match(/(([A-Z]\d\d\:\d\d\:\d\d\:\d\d)+)+/g);
of course if that pattern is actually good ;)
Write your regular expression to match a single instance, and use the global specifier /g to get a collection of matches (note the modification to regular expression):
var locs = txt.match(/([A-Z]\d\d\:\d\d\:\d\d\:\d\d)/g);
http://rubular.com/r/L2ZNyz2yJy
Indeed in this case the "+" sign is not necessary:
var locs = txt.match(/[A-Z]\d\d\:\d\d\:\d\d\:\d\d/g);
locs will be ["I01:00:00:05", "I01:00:00:04", "I01:00:00:03", "I01:00:00:02", "I01:00:00:01"]
.
Related
Hi,
I have this code:
var room = 'room2';
var exitroom = 'room1,room2,room3';
exitroom = exitroom.replace(/,${room},/,'');
console.log(exitroom);
you can try it here: https://jsfiddle.net/uq9w0Ls4/
my expected output is simply room1,room3 by taking room2 out but since it may change its position within the string I want to target the , no matter if it comes before or after the string but I cant figure out the regex logic here. I know I could just do simply:
var room = 'room2';
var exitroom = 'room1,room2,room3';
exitroom = exitroom.replace(room+',','').replace(','+room,'');
console.log(exitroom);
which works but I think regex would be a more direct approach.
Thank you.
First, by writing .replace(/,${room},/,'') you are not using the variable room.
To use a variable in a regex you should call new RegExp()
Second, if you want a regex that will match when the comma is before or after the word, you can use a group () with an Or | operator.
so it should look like this:
var reg = new RegExp(`(?:${room},|,${room})`, "g");
exitroom.replace(reg,'');
The ?: at the beginning of the group, is just so it should be a non-capturing group, it should work just fine also without it
I have a dynamically generated text like this
xxxxxx-xxxx-xxxxx-xxxxx-Map-B-844-0
How can I remove everything before Map ...? I know there is a hard coded way to do this by using substring() but as I said these strings are dynamic and before Map .. can change so I need to do this dynamically by removing everything before 4th index of - character.
You could remove all four minuses and the characters between from start of the string.
var string = 'xxxxxx-xxxx-xxxxx-xxxxx-Map-B-844-0',
stripped = string.replace(/^([^-]*-){4}/, '');
console.log(stripped);
I would just find the index of Map and use it to slice the string:
let str = "xxxxxx-xxxx-xxxxx-xxxxx-Map-B-844-0"
let ind = str.indexOf("Map")
console.log(str.slice(ind))
If you prefer a regex (or you may have occurrences of Map in the prefix) you man match exactly what you want with:
let str = "xxxxxx-xxxx-xxxxx-xxxxx-Map-B-844-0"
let arr = str.match(/^(?:.+?-){4}(.*)/)
console.log(arr[1])
I would just split on the word Map and take the first index
var splitUp = 'xxxxxx-xxxx-xxxxx-xxxxx-Map-B-844-0'.split('Map')
var firstPart = splitUp[0]
Uses String.replace with regex expression should be the popular solution.
Based on the OP states: so I need to do this dynamically by removing everything before 4th index of - character.,
I think another solution is split('-') first, then join the strings after 4th -.
let test = 'xxxxxx-xxxx-xxxxx-xxxxx-Map-B-844-0'
console.log(test.split('-').slice(4).join('-'))
Problem:
Extract image file name from CDN address similar to the following:
https://cdnstorage.api.com/v0/b/my-app.com/o/photo%2FB%_2.jpeg?alt=media&token=4e32-a1a2-c48e6c91a2ba
Two-stage Solution:
I am using two regular expressions to retrieve the file name:
var postLastSlashRegEx = /[^\/]+$/,
preQueryRegEx = /^([^?]+)/;
var fileFromURL = urlString.match(postLastSlashRegEx)[0].match(preQueryRegEx)[0];
// fileFromURL = "photo%2FB%_2.jpeg"
Question:
Is there a way I can combine both regular expressions?
I've tried using capture groups, but haven't been able to produce a working solution.
From my comment
You can use a lookahead to find the "?" and use [^/] to match any non-slash characters.
/[^/]+(?=\?)/
To remove the dependency on the URL needing a "?", you can make the lookahead match a question mark or the end of line indicator (represented by $), but make sure the first glob is non-greedy.
/[^/]+?(?=\?|$)/
You don't have to use regex, you can just use split and substr.
var str = "https://cdnstorage.api.com/v0/b/my-app.com/o/photo%2FB%_2.jpeg?alt=media&token=4e32-a1a2-c48e6c91a2ba".split("?")[0];
var fileName = temp.substr(temp.lastIndexOf('/')+1);
but if regex is important to you, then:
str.match(/[^?]*\/([^?]+)/)[1]
The code using the substring method would look like the following -
var fileFromURL = urlString.substring(urlString.lastIndexOf('/') + 1, urlString.lastIndexOf('?'))
I have a simple question:
How do I use RegExp in Javascript to find strings that matches this filter:
*[0-9].png in order to filter out file sequences.
For example:
bird001.png
bird002.png
bird003.png
or
abc_1.png
abc_2.png
Should ignore strings like abc_1b.png and abc_abc.png
I'm going to use it in a getFiles function.
var regExp = new RegExp(???);
var files = dir.getFiles(regExp);
Thanks in advance!
EDIT:
If I have a defined string, let's say
var beginningStr = "bird";
How can I check if a string matches the filter
beginningStr[0-9].png
? And ideally beginningString without case sensitivity. So that the filter would allow Bird01 and bird02.
Thanks again!
Anything followed by [0-9] and ened by .png:
/^.*[0-9]\.png$/i
Or simply without begining (regex will find it itself):
/[0-9]\.png$/i
If I understood correctly, you need a regex that matches files with names which:
Begin with letters a-z, A-Z
Optionally followed with single _
Followed by one or more digits
Ending with .png
Regex for this is [a-zA-Z]_{0,1}+\d+\.png
You could try online regex builders which offer immediate explanation of what you write.
If I understood correctly,
var re = /\s[a-zA-Z]*[0-9]+\.png/g;
var filesArr = str.match(re);
filesArr.sort();// you can use own sort function
Please specify what is the dir variable
I have string like "abcdefecg asdasda(SIAD) (EA91)" what i need is to get EA91.
I use regular expression to get content in parens but this give me only first existence.
var regExpTextBetweenBraces = /\(([^)]+)\)/;
var res = "abcdefecg asdasda (SIAD) (EA91)".match(regExpTextBetweenBraces);
res[1] will contain SIAD, but i need EA91. How can i do this. Any help ?
Just use the global g flag on your regex:
var regExpTextBetweenBraces = /\(([^)]+)\)/g;
Try /\([^)]+\).*?\(([^)]+)\)/ It'll look for a (something) (Matched) all you needed to do was to tell it to not match the first brace. Or you could use a global match and use an index.