如何仅从字符串中提取数字,但数字开头带有特殊字符的子字符串除外

我的计划是从字符串中提取数字,除了带有特殊字符的数字。我的意思?请想象一下以下内容(如Excel公式):

=$A12+A$345+A6789

我需要提取数字,其中开头不存在任何字符$,因此右正则表达式的结果应该是:

12
6789

我做了一些调查,我使用了以下正则表达式:

/[A-Z][0-9]+(?![/W])/g

其中摘录:

A12
A6789

我正在考虑使用嵌套的正则表达式(另外从该结果中提取数字),但我不知道这是否可能。到目前为止,我的源代码是脚本:

http://jsfiddle.net/janzitniak/fvczu7a0/7/


冉冉说
浏览 151回答 2
2回答

撒科打诨

const charSequence = '=$A12+A$345+A6789';const numberList = (charSequence&nbsp; .split(/\$\d+/)&nbsp; &nbsp; &nbsp; &nbsp;// - split at "'$' followed by one or more numbers".&nbsp; .join('')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// - join array of split results into string again.&nbsp; .match(/\d+/g) || []) // - match any number-sequence or fall back to empty array.&nbsp; .map(str => +str);&nbsp; &nbsp; // - 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/matchconst charSequence = '=$A13+A$345+A6790';const numberList = (charSequence&nbsp; .split(/\$\d+/)&nbsp; &nbsp; &nbsp; &nbsp;// - split at "'$' followed by one or more numbers".&nbsp; .join('')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// - join array of split results into string again.&nbsp; .match(/\$*[A-Z]\d+/g) || []);&nbsp; // - match any sequence of an optional '$' followed&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp;by 1 basic latin uppercase character followed&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp;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&nbsp; // - split at "'$' followed by one or more numbers".&nbsp; .split(/\$\d+/)&nbsp; // - join array of split results into string again.&nbsp; .join('')&nbsp; // - match any sequence of an optional '$' followed by 1 basic latin&nbsp;&nbsp; //&nbsp; &nbsp;uppercase character followed by one or more number character(s)&nbsp; //&nbsp; &nbsp;and store each capture into a named group.&nbsp; .matchAll(/(?<anchor>\$*[A-Z])(?<integer>\d+)/g) || [])&nbsp; // 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&nbsp; // - split at "'$' followed by 1 basic latin uppercase character&nbsp;&nbsp; //&nbsp; &nbsp;followed by one or more number character(s)".&nbsp; .split(/\$[A-Z]\d+/)&nbsp; // - join array of split results into string again.&nbsp; .join('')&nbsp; // - match any sequence of 1 basic latin uppercase character&nbsp; //&nbsp; &nbsp;followed by an optional '$' followed by one or more number&nbsp; //&nbsp; &nbsp;character(s) and store each capture into a named group.&nbsp; .matchAll(/(?<column>[A-Z])(?<anchor>\$*)(?<row>\d+)/g) || [])&nbsp; // map each regexp result from a list of RegExpStringIterator entries.].map(({ groups }) => [&nbsp; // - be aware that "Z" (charCode:90) will be followed by "[" (charCode:91)&nbsp; // - thus, the handling of this edge case still needs to be implemented.&nbsp; String.fromCharCode(groups.column.charCodeAt(0) + 1),&nbsp; groups.anchor,&nbsp; groups.row].join(''));console.log('listOfIncrementedColumns : ', listOfIncrementedColumns);.as-console-wrapper { min-height: 100%!important; top: 0; }

翻过高山走不出你

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]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript