Это выражение может сделать это, и мы, безусловно, можем добавить больше границ, если необходимо:
^([A-B=]+\s)([0-9]+|)
У нас есть две группы захвата, которые мы можем просто назвать их, используя $1
и $2
.
Graph
Этот график показывает, как будет работать выражение, и мы можем визуализировать другие выражения в этой ссылке :
![enter image description here](https://i.stack.imgur.com/KTpSX.png)
Редактировать:
Тогда это выражение может помочь нам сделать это, создав 3 группы захвата:
^([A-Z]+)([=\s]+)([A-z0-9-]+)
![enter image description here](https://i.stack.imgur.com/LrwaJ.png)
Тест на RegEx 1
const regex = /^([A-B=]+\s)([0-9]+|)/gm;
const str = `"
......
A=
B= 12345
....."`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
Тест на RegEx 2
const regex = /^([A-Z]+)([=\s]+)([A-z0-9-]+)/gm;
const str = `ADFJE = 12313-asrn[5493]h`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}