How would I construct a regular expression to yield the index of an alphanumeric character that is preceded by a non-alphanumeric character?
Examples:
- (Special Edition) --> should match only 'S' and yield an index of 1
- Special Edition --> should not match at all
- "{(Special Edition)}" --> should match 'S' and yield an index of 3
I know that JavaScript can provide the index of the match like so:
const index = string.match(/regular_expression/)?.index || 0;
I cannot figure out how to construct the an expression that does what I have described.
CodePudding user response:
If this part (S
should yield index 1, I think this part {(S
should yield index 2 instead of 3.
You can match both characters and capture the second character using a capture group, and then add 1 to get the index.
You can use a lookbehind assertion if that is supported and then you don't need the extra addition of 1.
As a pattern you can match:
[^a-zA-Z\d\s]([a-zA-Z\d])
The pattern matches:
[^a-zA-Z\d\s]
Match a single char not being alphanumeric or a whitespace char([a-zA-Z\d])
Capture a single alphanumeric char in group 1
See a regex demo.
const regex = /[^a-zA-Z\d\s]([a-zA-Z\d])/;
[
"(Special Edition)",
"Special Edition",
"{(Special Edition)}",
].forEach(s => {
const m = s.match(regex);
if (m) {
console.log(`${s} --> ${m[1]} index: ${m.index 1}`);
}
})
CodePudding user response:
Use positive lookbehind and String.prototype.search
method:
'(Special Edition)'.search(/(?<=[^A-Za-z0-9\s])[A-Za-z0-9]/); // 1
'Special Edition'.search(/(?<=[^A-Za-z0-9\s])[A-Za-z0-9]/); // -1
'{(Special Edition)}'.search(/(?<=[^A-Za-z0-9\s])[A-Za-z0-9]/); // 2