2 回答

TA貢獻1934條經(jīng)驗 獲得超2個贊
const charSequence = '=$A12+A$345+A6789';
const numberList = (charSequence
.split(/\$\d+/) // - split at "'$' followed by one or more numbers".
.join('') // - join array of split results into string again.
.match(/\d+/g) || []) // - match any number-sequence or fall back to empty array.
.map(str => +str); // - typecast string into number.
//.map(str => parseInt(str, 10)); // parse string into integer.
console.log('numberList : ', numberList);
.as-console-wrapper { min-height: 100%!important; top: 0; }
@ibraheem你能再次幫助我嗎?如果我想得到以下結果[“$A 13”,“A6790”],如何增加ref輸出?- 揚·齊特尼亞克 23分鐘前
...該方法可以非??焖俚氐?,因此它被證明是相當靈活的。split/join/match
const charSequence = '=$A13+A$345+A6790';
const numberList = (charSequence
.split(/\$\d+/) // - split at "'$' followed by one or more numbers".
.join('') // - join array of split results into string again.
.match(/\$*[A-Z]\d+/g) || []); // - match any sequence of an optional '$' followed
// by 1 basic latin uppercase character followed
// by one or more number character(s).
console.log('numberList : ', numberList);
.as-console-wrapper { min-height: 100%!important; top: 0; }
彼得感謝您對增量的快速響應,但在開始時,我有常量字符序列=“=$A 12 + A $ 345 + A6789”;并作為輸出,我需要[“$A 13”,“A6790”]。- 揚齊特尼亞克
...好吧,最后一個人將全面了解整個問題...這是(1)擺脫不必要的模式...(2)在特定模式中匹配數(shù)字,并以某種方式記住后者(3)增加這些數(shù)字,并以某種方式將它們重新設計成記憶/可回憶的模式。
const anchorSequence = '=$A12+A$345+A6789';
const listOfIncrementedAnchorCoordinates = [...(anchorSequence
// - split at "'$' followed by one or more numbers".
.split(/\$\d+/)
// - join array of split results into string again.
.join('')
// - match any sequence of an optional '$' followed by 1 basic latin
// uppercase character followed by one or more number character(s)
// and store each capture into a named group.
.matchAll(/(?<anchor>\$*[A-Z])(?<integer>\d+)/g) || [])
// map each regexp result from a list of RegExpStringIterator entries.
].map(({ groups }) => `${ groups.anchor }${ (+groups.integer + 1) }`);
console.log('listOfIncrementedAnchorCoordinates : ', listOfIncrementedAnchorCoordinates);
.as-console-wrapper { min-height: 100%!important; top: 0; }
彼得,如果你是感興趣的(...ed)在另一個問題中,我有一個。如何更改常量錨點序列 = '=$A 12+A$345+A6789';到以下輸出 [“B$345”,“B6789”]?我的意思是,如果字母不以$開頭,則按字母順序將字母更改為下一個字母(如果是A,則更改為B,如果是B,則更改為C,依此類推)。在我的例子中,它應該只改變A$345和A6789。- 揚齊特尼亞克
...通過一些思考努力,迭代/重構之前的版本到最后一個版本并不難......
const anchorSequence = '=$A12+A$345+A6789';
const listOfIncrementedColumns = [...(anchorSequence
// - split at "'$' followed by 1 basic latin uppercase character
// followed by one or more number character(s)".
.split(/\$[A-Z]\d+/)
// - join array of split results into string again.
.join('')
// - match any sequence of 1 basic latin uppercase character
// followed by an optional '$' followed by one or more number
// character(s) and store each capture into a named group.
.matchAll(/(?<column>[A-Z])(?<anchor>\$*)(?<row>\d+)/g) || [])
// map each regexp result from a list of RegExpStringIterator entries.
].map(({ groups }) => [
// - be aware that "Z" (charCode:90) will be followed by "[" (charCode:91)
// - thus, the handling of this edge case still needs to be implemented.
String.fromCharCode(groups.column.charCodeAt(0) + 1),
groups.anchor,
groups.row
].join(''));
console.log('listOfIncrementedColumns : ', listOfIncrementedColumns);
.as-console-wrapper { min-height: 100%!important; top: 0; }

TA貢獻1875條經(jīng)驗 獲得超3個贊
const regex = /(?<ref>\$?[A-Z]+(?<!\$)[0-9]+)/g;
const str = `=$A12+A$345+A6789`;
const refs = [...(str.matchAll(regex) || [])].map(result => result.groups.ref);
console.log(refs)
匹配任何包含 A-Z 的字符串,該字符串前面有 $ 0 或 1 次,后跟 0-9 一次或多次,但不在 $ 前面,全部后跟 + 零或一次。
您可以忽略所有匹配的組,但捕獲所需的組,引用為(您可以隨意調用它)。ref
輸出:
["$A12","A6789"]
如果您只想要數(shù)字部分,則可以使用:
const regex = /\$?[A-Z]+(?<!\$)(?<num>[0-9]+)/g;
const str = `=$A12+A$345+A6789`;
const nums = [...(str.matchAll(regex) || [])].map(result => +result.groups.num);
console.log(nums)
輸出:
[12, 6789]
添加回答
舉報