Assume there is the string
just/the/path/to/file.txt
I need to get the part between the first and the last slash: the/path/to
I came up with this regex: /^(.*?).([^\/]*)$/, but this gives me everything in front of the last slash.
Don't use [^/]*, since that won't match anything that contains a slash. Just use .* to match anything:
/(.*?)\/(.*)\/(.*)/
Group 1 = just, Group 2 = the/path/to and Group 3 = file.txt.
The regex should be \/(.*)\/. You can check my below demo:
const regex = /\/(.*)\//;
const str = `just/the/path/to/file.txt`;
let m;
if ((m = regex.exec(str)) !== null) {
console.log(m[1]);
}
This regex expression will do the trick
const str = "/the/path/to/the/peace";
console.log(str.replace(/[^\/]*\/(.*)\/[^\/]*/, "$1"));
[^\/]*\/(.*)\/[^\/]*
If you are interested in only matching consecutive parts with a single / and no //
^[^/]*\/((?:[^\/]+\/)*[^\/]+)\/[^\/]*$
^ Start of string
[^/]*\/ Negated character class, optionally match any char except / and then match the first /
( Capture group 1
(?:[^\/]+\/)* Optionally repeat matching 1+ times any char except / followed by matching the /
[^\/]+ Match 1+ times any char except /
) Close group 1
\/[^\/]* Match the last / followed by optionally matching any char except /
$ End of string
Regex demo
const regex = /^[^/]*\/((?:[^\/]+\/)*[^\/]+)\/[^\/]*$/;
[
"just/the/path/to/file.txt",
"just/the/path",
"/just/",
"just/the/path/to/",
"just/the//path/test",
"just//",
].forEach(str => {
const m = str.match(regex);
if (m) {
console.log(m[1])
};
});
Related
const regex = /[1-9a-zA-Z]{3}-[1-9a-zA-Z]{3}-[1-9a-zA-Z]{3}/gm;
let m;
while ((m = regex.exec(tweet.text)) !== null) {
let newClass = tweet.text.replace(/[^1-9a-zA-Z]{3}-[^1-9a-zA-Z]{3}-[^1-9a-zA-Z]{3}/g, '');
console.log(`Found match: ${newClass}`);
};
when tweet.text = "123.qwe.456 test" I still get the same output but I want to remove anything which doesnt fit the pattern
/[1-9a-zA-Z]{3}-[1-9a-zA-Z]{3}-[1-9a-zA-Z]{3}/
any ideas?
You can use capture groups to extract exactly what gets matched in your string and then replace your original variable with this value. Something like
const regex = /([1-9a-zA-Z]{3}-[1-9a-zA-Z]{3}-[1-9a-zA-Z]{3})/
let match = tweet.text.match(regex)
tweet.text = match[1]
Instead of replace, you can get the match instead
\b[1-9a-zA-Z]{3}([-.])[1-9a-zA-Z]{3}\1[1-9a-zA-Z]{3}\b
Explanation
\b A word boundary
[1-9a-zA-Z]{3} Match 3 times any of the listed (Note that 1-9 does not match a 0)
([-.]) Capture in group 1 either an - or .
[1-9a-zA-Z]{3} Match 3 times any of the listed
\1 Back reference to group 1, match the same as captured in group 1
[1-9a-zA-Z]{3} Match 3 times any of the listed
\b A word boundary
Regex demo
const regex = /[1-9a-zA-Z]{3}-[1-9a-zA-Z]{3}-[1-9a-zA-Z]{3}/gm;
let m;
while ((m = regex.exec(tweet.text)) !== null) {
console.log(`Found match: ${m[0]}`);
figured the solution
I have an input string
var input = 'J1,J2, J3';
I'm using the following pattern to extract the group value
var regex = /(,? ?(?<JOUR>J[0-9]+)+)/
while extracting the groups as below
var match = regex.exec(input);
match.groups contains only one group. How can i get all the groups J1 J2 and J3 from the input string ?
You can use .match of string to get groups
input.match(/J[0-9]+/g)
var input = 'J1,J2, J3';
console.log(input.match(/J[0-9]+/gi))
Match a capital J, then any amount of numbers:
var input = 'J1,J2, J3';
var regex = /J[0-9]+/g;
console.log(input.match(regex));
You could take the start of the string and the comma with an optional space into account and remove the outer group to use only 1 capturing group. To prevent the digits being part of a larger word you might add a word boundary \b
Note that you can omit the quantifier+ after )+ because that will repeat the group and will give you only the value of the last iteration.
(?:^|[,-] ?)(?<JOUR>J[0-9]+)\b
(?:^|[,-] ?) Match either the start of the string or comma or hyphen with an optional space
(?<JOUR>J[0-9]+) Named capture group JOUR, match J and then 1+ digits
\b Word boundary to prevent the digits being part of a larger word
Regex demo
Use exec to get the value from the first capturing group
const regex = /(?:^|, ?)(?<JOUR>J[0-9]+\b)+/g;
let m;
[
"J1, J2, J3 - J5, J7",
"J1,J2, J3"
].forEach(str => {
while ((m = regex.exec(str)) !== null) {
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
console.log(m[1]);
}
});
const input = 'J1,J2, J3,J10';
const regexJfollowOneDigit = /(J\d{1}(?!\d))/g
const regexJfollowOneOrMoreDigit = /(J\d+)/g
console.log(input.match(regexJfollowOneDigit))
console.log(input.match(regexJfollowOneOrMoreDigit))
I am trying to capture all characters between multiple instances of asterisks, which are comma delimited in a string. Here's an example of the string:
checkboxID0*,*checkboxID1*,&checkboxID2&,*checkboxID3*,!checkboxID4!,checkboxID5*
The caveat is that the phrase must start and end with an asterisk. I have been able to come close by using the following regex, however, it won't discard any matches when the captured string is missing the starting asterisk(*):
let str = "checkboxID0*,*checkboxID1*,&checkboxID2&,*checkboxID3*,!checkboxID4!,checkboxID5*"
const regex = /[^\,\*]+(?=\*)/gi;
var a = str.match(regex)
console.log(a) // answer should exclude checkboxID0 and checkboxID5
The answer returns the following, however, "checkboxID0 and checkboxID5" should be excluded as it doesn't start with an asterisk.
[
"checkboxID0",
"checkboxID1",
"checkboxID3",
"checkboxID5"
]
Thanks, in advance!
You need to use asterisks on both ends of the pattern and capture all 1 or more chars other than commas and asterisks in between:
/\*([^,*]+)\*/g
See the regex demo
Pattern details
\* - an asterisk
([^,*]+) - Capturing group 1: one or more chars other than , and *
\* - an asterisk
JS demo:
var regex = /\*([^,*]+)\*/g;
var str = "checkboxID0*,*checkboxID1*,&checkboxID2&,*checkboxID3*,!checkboxID4!,checkboxID5*";
var m, res = [];
while (m = regex.exec(str)) {
res.push(m[1]);
}
console.log(res);
I have a string aa,bb\\,cc,dd. I need to split it by comma but only when the previous character is not backslash. So what I want is:
aa
bb\\,cc
dd
Since JavaScript regular expression doesn't support negative look back, I want to know how to solve it in this case. Thank you.
You may use this regex for match:
/(?=.)([^,\\]*(?:\\.[^,\\]*)*)(?:,|$)/gm
This regex ignores all escaped characters while matching substrings that have a comma or end of line at next positions.
RegEx Demo
RegEx Details:
(?=.): Make sure we don't match empty strings
([^,\\]*: Match 0 or more of any chars that are not , and \
(?:\\.[^,\\]*)*): Match \ followed by escaped character and then 0 or more of any chars that are not , and \.
(?:,|$): Match comma or end of line
const regex = /(?=.)([^,\\]*(?:\\.[^,\\]*)*)(?:,|$)/gm;
const str = `aa,bb\\,cc,dd`;
let m;
while ((m = regex.exec(str)) !== null) {
console.log(m[1]);
}
//=> [aa, bb\,cc, dd]
If you want to split by comma with a lookbehind assertion, you can split by all commas and then enforce the lookbehind assertion in a .reduce() while accumulating the array.
const str = 'aa,bb\\,cc,dd'
const values = str.split(/,/g).reduce((acc, str) => {
const lastIndex = acc.length - 1
if (lastIndex >= 0 && acc[lastIndex].endsWith('\\')) {
acc[lastIndex] += str
} else {
acc.push(str)
}
return acc
}, [])
console.log(values)
I have the following regex:
/a/b/([^\/]+)(\?id=1)?
The match for the first captured group is /a/b/search?id=1.
Currently, Regexp.$1=search?id=1, but I would like Regexp.$1=search.
You may restrict the [^\/] by adding ? into the negated character class:
/\/a\/b\/([^\/?]+)(\?id=1)?/
^
See the regex demo. The [^\/?]+ will match 1 or more chars other than / and ? and will thus stop before a ?, that will be matched with an optional capturing group at the end of the pattern.
JS demo:
var s = "/a/b/search?id=1";
var rx = /\/a\/b\/([^\/?]+)(\?id=1)?/;
var m = s.match(rx);
if (m) {
console.log(m[1]); // => search
}