簡體   English   中英

拆分帶有數字問題的正則表達式字符串

[英]Split string with regex with numbers problems

有一個字符串列表,如:

Client Potential XSS2Medium
Client HTML5 Insecure Storage41Medium
Client Potential DOM Open Redirect12Low

我想將每個字符串分成三個字符串,如:

["Client Potential XSS", "2", "Medium"]

我用這個正則表達式:

/[a-zA-Z ]+|[0-9]+/g)

但是對於包含其他數字的字符串,它顯然不起作用。 例如:

Client HTML5 Insecure Storage41Medium

結果是:

["Client HTML", "5", " Insercure Storage", "41", "Medium"]

我找不到產生的正則表達式:

["Client HTML5 Insercure Storage", "41", "Medium"]

這個正則表達式適用於regex101.com:

(.+[ \t][A-z]+)+([0-9]+)+([A-z]+)

在我的代碼中使用它:

data.substring(startIndex, endIndex)
        .split("\r\n") // Split the vulnerabilities
        .filter(item => !item.match(/(-+)Page \([0-9]+\) Break(-+)/g) // Remove page break
          && !item.match(/PAGE [0-9]+ OF [0-9]+/g) // Remove pagination
          && item !== '') // Remove blank strings
        .map(v => v.match(/(.+[ \t][A-z]+)+([0-9]+)+([A-z]+)/g));

不起作用。

任何幫助將不勝感激!

編輯:所有字符串以HighMediumLow

問題出在你的g全球旗幟上。

從這一行刪除該標志: .map(v => v.match(/(.+[ \\t][Az]+)+([0-9]+)+([Az]+)/g)); 做到:

.map(v => v.match(/(.+[ \\t][Az]+)+([0-9]+)+([Az]+)/));


另外,你可以使正則表達式更簡單,如@bhmahler 所示

.map(v => v.match(/(.*?)(\\d+)(low|medium|high)/i));

以下正則表達式應該為您提供所需的內容。

/(.*?)(\d+)(low|medium|high)/gi

以下是https://regex101.com/r/AS9mvf/1的示例

這是一個使用map的例子

 var entries = [ 'Client Potential XSS2Medium', 'Client HTML5 Insecure Storage41Medium', 'Client Potential DOM Open Redirect12Low' ]; var matches = entries.map(v => { var result = /(.*?)(\\d+)(low|medium|high)/gi.exec(v); return [ result[1], result[2], result[3] ]; }); console.log(matches); 

您可以使用解決方法(匹配與捕獲,然后替換):

 let strings = ['Client Potential XSS2Medium', 'Client HTML5 Insecure Storage41Medium', 'Client Potential DOM Open Redirect12Low', 'Client HTML5 Insecure Storage41Medium']; let regex = /(?:HTML5|or_other_string)|(\\d+)/g; strings.forEach(function(string) { string = string.replace(regex, function(match, g1) { if (typeof(g1) != "undefined") { return "#@#" + g1 + "#@#"; } return match; }); string = string.split("#@#"); console.log(string); }); 

請參閱regex101.com上的其他演示

在這里,您有一個解決方案,使用String.replace()將單詞HighLowMedium之前的數字包裝在自定義token ,最后通過此token拆分生成的字符串:

 const inputs = [ "Client Potential XSS2High", "Client HTML5 Insecure Storage41Medium", "Client Potential DOM Open Redirect12Low" ]; let token = "-#-"; let regexp = /(\\d+)(High|Low|Medium)$/; let res = inputs.map( x => x.replace(regexp, `${token}$1${token}$2`).split(token) ); console.log(res); 

另一種解決方案是使用這個正則表達式:/ /^(.*?)(\\d+)(High|Low|Medium)$/i

 const inputs = [ "Client Potential XSS2High", "Client HTML5 Insecure Storage41Medium", "Client Potential DOM Open Redirect12Low" ]; let regexp = /^(.*?)(\\d+)(High|Low|Medium)$/i; let res = inputs.map( x => x.match(regexp).slice(1) ); console.log(res); 

 let arr = ["Client Potential XSS2Medium", "Client HTML5 Insecure Storage41Medium", "Client Potential DOM Open Redirect12Low"]; let re = /^.+[a-zA-Z](?=\\d+)|\\d+(?=[AZ])|[^\\d]+\\w+$/g; arr.forEach(str => console.log(str.match(re))) 

^.+[a-zA-Z](?=\\d+)匹配字符串的開頭,后跟a-zA-Z后跟一個或多個數字字符

\\d+(?=[AZ])匹配一個或多個數字字符,后跟大寫字母字符

[^\\d]+\\w+$否定數字字符,然后匹配單詞字符,直到字符串結尾

 const text = `Client Potential XSS2Medium Client HTML5 Insecure Storage41Medium Client Potential DOM Open Redirect12Low` const res = text.split("\\n").map(el => el.replace(/\\d+/g, a => ' ' + a + ' ') ); console.log(res) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM