Yep nope - you’re right. Doing without the lookaheads is actually consuming the letters. Only workaround for that would be to go letter by letter. So yep, the lookaheads and lookbehinds, which are a painful addition to an already rough regex. Here’s how it looks: https://replit.com/@TobiasParent/plusSignRegexQuestion#index.js
Maybe is better to go letter by letter, and simply check if the pre/post of that letter are plus?
(twenty minutes later…) Tried the pre-post check in that same repl, called it
plusTwo. They both work, I think. The trick with that one is, as @gururajankappa pointed out at the beginning, you need to check if both the first or last are a letter, and return false immediately in that case.
Next, you loop from the second to the next-to-last, checking if the current character is a string and if it is, that either the preceding or following are not a plus. In that case, we use the early-return pattern to return false.
If we get to the end of the string without returning, then every case was valid, and we can return true.
that was a fun challenge!
@gururajankappa, the issues I see looking back at your code is the
for...in, which doesn’t apply to arrays or array-like objects (strings are “array-like”). You could use a simple
for loop, or
for...of. In my case, a for loop worked fine.
Next, I’m not sure why you’re checking if the given character is
NaN - I guess that’s a way to see if it’s a letter (or a plus, or a pound-sign, or something else). I simply used a regex,
/[a-z]/i to check. the
i on that tell the regex that it’s case-insensitive. If you find a match, then you check if it has an index+1 and index-1 (which I took care of by starting my loop at the second letter), then you check if those are both plus.
But within that
for loop, you also don’t console.log true or false, or return a true or false from your function… which would likely also be an issue.