2 回答
TA贡献1934条经验 获得超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)在特定模式中匹配数字,并以某种方式记住后者(3)增加这些数字,并以某种方式将它们重新设计成记忆/可回忆的模式。
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条经验 获得超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"]
如果您只想要数字部分,则可以使用:
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]
添加回答
举报
