Regex Expression To Cut String At Nth Occurrence Of Character And Return First Part Of String
I have found the answer for a case which returns the second part of the string, eg: 'qwe_fs_xczv_xcv_xcv_x'.replace(/([^\_]*\_){**nth**}/, ''); - where is nth is the amount of occu
Solution 1:
You need to use end anchor($
) to assert ending position.
"qwe_fs_xczv_xcv_xcv_x".replace(/(_[^_]*){nth}$/, '');
// --------------------^-----------^--- here
console.log(
"qwe_fs_xczv_xcv_xcv_x".replace(/(_[^_]*){3}$/, '')
)
UPDATE : In order to get the first n segments you need to use String#match
method with slight variation in the regex.
"qwe_fs_xczv_xcv_xcv_x".match(/(?:(?:^|_)[^_]*){3}/)[0]
console.log(
"qwe_fs_xczv_xcv_xcv_x".match(/(?:(?:^|_)[^_]*){3}/)[0]
)
(?:^|_)
helps to assert the start position or matching the leading _
.
Regex explanation here.
Another alternative for the regex would be, /^[^_]*(?:_[^_]*){n-1}/
. Final regex would be like:
/^[^_]*(?:_[^_]*){2}/
console.log(
"qwe_fs_xczv_xcv_xcv_x".match(/^[^_]*(?:_[^_]*){2}/)[0]
)
Solution 2:
Use String.match()
to look from the start (^
) of the string, for three sequences of characters without underscore, that might start with an underscore (regex101):
const result = "qwe_fs_xczv_xcv_xcv_x".match(/^(?:_?[^_]+){3}/);
console.log(result);
Solution 3:
If you want to use replace
, then capture up to right before the third _
and replace with that group:
const re = /^(([^_]*_){2}[^_]*(?=_)).*$/;
console.log("qwe_fs_xczv_xcv_xcv_x".replace(re, '$1'));
console.log("qwe_fs_xczv_xcv_xcv_x_x_x".replace(re, '$1'));
But it would be nicer to use match
to match the desired substring directly:
const re = /^([^_]*_){2}[^_]*(?=_)/;
console.log("qwe_fs_xczv_xcv_xcv_x".match(re)[0])
console.log("qwe_fs_xczv_xcv_xcv_x_x_x".match(re)[0])
Post a Comment for "Regex Expression To Cut String At Nth Occurrence Of Character And Return First Part Of String"