StarshipladDev
StarshipladDev

Reputation: 1145

Regex For Every Second Letter In Individual words

I'm attempting to implement a regex query that can select every second letter (per word) so that I can action the returned values.

I have attempted the following using This sandbox but nothing has been successful.

\w([A-Za-z]) ,\w(\w) , \w{1}(\w){1}

What would the correct regex expression be to select every second letter of each word.

As an example:

H[i] t[h]e[r]e s[t]a[c]k[o]v[e]r[f]l[o]w

Upvotes: 0

Views: 604

Answers (2)

DecPK
DecPK

Reputation: 25408

You can simply select all character that comes after a character.

[a-zA-Z]([a-zA-Z])

enter image description here

const str = "Hi there stackoverflow";
const result = [...str.matchAll(/[a-zA-Z]([a-zA-Z])/g)].map((arr) => arr[1]);
console.log(result);

Upvotes: 2

This simplest solution would be to match for \w and ignore the every other match from the result

Upvotes: 0

Related Questions