Positive Lookahead

Tell us what’s happening:

How the below expression matches this string thisIsSpinalTap. Since here I does not have a space or _ before so how does it match ?

Your code so far

return str.split(/\s|_|(?=[A-Z])/)

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36.

Challenge: Spinal Tap Case

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/spinal-tap-case

this part here match before a capital letter
see the purple line (it is where it matches):
image

please stop putting solutions without spoiler tags - it is the second one today

I am so sorry. I thought i can put a single line of code without that tag

x(?=y) – positive lookahead (matches 'x' when it's followed by 'y')Since x is not present how it is valid ?

x(?=y) matches the x if it is followed by y
(?=y) matches the point before the y
you can play with an online tester if it can help clear your ideas in how things work: https://regex101.com/

const consonantCluster = str.match(/^[^aeiou]+/)[0]

What is this weird syntax at the end you can see [0], when i remove zero output remains the same

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/pig-latin

(?=y) matches the point before the y I tried to find it on MDN but this reference was not available. Where did u get it from ?

it is what happens, try on the regex tester online, try to do "xyxxyyyxux".split(/(?=y)/), it will split before the y


Matches at a position where the pattern inside the lookahead can be matched. Matches only the position. It does not consume any characters or expand the match. In a pattern like one(?=two)three, both two and three have to match at the position where the match of one ends.

source: https://regular-expressions.mobi/refadv.html?wlr=1


match returns an array, with the [0] it is selecting the first element in the array from match

1 Like