I would like to have a Regex select the next word after brackets containing letters. There can be more than multiple brackets following each other before the next word.
Example:
[X]word[y][Z]another
Expected output: word, another
I would like also to achieve the same but providing the regex the content between the brackets and get the following word:
Example 2: i would like the next word after [y]:
[X]word[y][Z]another
Expected output: another
I tried (?m)^\[[^\]\[]*\]\K.
but somehow it's not working.
Thank you in advance
CodePudding user response:
Javascript does not support \K
or inline modifiers like (?m)
You might use for example a capture group for 1 or more word characters, and first match from [...]
\[[^\][]*](\w )
Or providing the regex the content between the brackets and capture the following word, by optionally repeating [...]
after it:
\[y](?:\[[^\][]*])*(\w )
CodePudding user response:
This:
.*\[y\](?:\[[a-zA-Z]*\])*([a-zA-Z]*)(?:\[.*)
will capture the next word after the bracket which contains the specified character (here [y] as an example), excluding all brackets and stuff inside of them.
Example:
[X]word[y][i][ee][Z]another
=> group1 = "another"
[X]word[y][i][ee][Z]another[eer]eeee[ee]
=> group1 = "another"
and so on.
When asking these type of questions, I suggest you put as many examples as you can, it helps a lot with figuring out what you mean
if you want to change the input character, you can change "y" to whichever character you need in the regex. It should be possible to do programatically
CodePudding user response:
Here is one regex approach. We can eagerly try to match a term in [...]
, and that failing, match a single word. Then, we can filter off the terms matched in square brackets, leaving behind the words we want to match.
var input = "[X]word[y][Z]another";
var matches = input.match(/\[.*?\]|\w /g)
.filter(x => !x.match(/^\[.*\]$/));
console.log(matches);
CodePudding user response:
For some reason, all the suggested RegEx skip the characters ä ë ü ö
which actually are in the words I have after the brackets. I've tried to play with the proposed solutions, but no luck...