第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何僅從字符串中提取數(shù)字,但數(shù)字開頭帶有特殊字符的子字符串除外

如何僅從字符串中提取數(shù)字,但數(shù)字開頭帶有特殊字符的子字符串除外

冉冉說 2022-09-23 17:34:08
我的計劃是從字符串中提取數(shù)字,除了帶有特殊字符的數(shù)字。我的意思?請想象一下以下內容(如Excel公式):=$A12+A$345+A6789我需要提取數(shù)字,其中開頭不存在任何字符$,因此右正則表達式的結果應該是:12 6789我做了一些調查,我使用了以下正則表達式:/[A-Z][0-9]+(?![/W])/g其中摘錄:A12 A6789我正在考慮使用嵌套的正則表達式(另外從該結果中提取數(shù)字),但我不知道這是否可能。到目前為止,我的源代碼是腳本:http://jsfiddle.net/janzitniak/fvczu7a0/7/
查看完整描述

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; }


查看完整回答
反對 回復 2022-09-23
?
翻過高山走不出你

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]


查看完整回答
反對 回復 2022-09-23
  • 2 回答
  • 0 關注
  • 122 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號