Regex: positive look behind
28 August 2022 (Updated 4 September 2022)
On this page
Match a q
that’s immediately preceded by a u
, without making u
part of the match:
(?<=u)q
Example usages
Example 1
Solution to a kata that’s described as:
You are given a String of one or more words. Your task is to check the length of each word and if it’s less than 4, you keep it unmodified otherwise you find out the first vowel which is coming after 3-rd character and replace vowel and rest of word with a dot. Finally, you should return the same string but shorten (only if the length of words is more then 3 characters).
function shortenSpeech(str) {
return str
.split(' ')
.map(word => {
if (word.length < 4 || !/[aeiou]/.test(word)) {
return word
}
return word.replace(/(?<=.{3})[aeiou].*/, '.')
})
.join(' ')
}
Tagged:
Regex
Thanks for your comment 🙏. Once it's approved, it will appear here.
Leave a comment