从字符串regex中提取多个值

时间:2016-07-09 13:13:06

标签: javascript jquery regex extract

我有字符串:

"BSSID:00:00:00:00:00:00<br>Encryption: WPA+PSK WPA+AES-CCM<br>Channel: 11<br><br>Current Clients: <br><br><br>"

我无法弄清楚如何使用正则表达式过滤掉&#34; BSSID&#34;,&#34;加密&#34;的值。等等。 我试过这个正则表达式:\b(BSSID:|Encryption: |Channel: )\b 但是,这并不像想象的那样有效。如果有人能让我走向正确的方向,那就太棒了!感谢。

2 个答案:

答案 0 :(得分:1)

将以下正则表达式与String.replace函数一起使用:

var str = "BSSID:00:00:00:00:00:00<br>Encryption: WPA+PSK WPA+AES-CCM<br>Channel: 11<br><br>Current Clients: <br><br><br>",
    replaced = str.replace(/\b(BSSID|Encryption|Channel)\s?:/g, "");

console.log(replaced);  // 00:00:00:00:00:00<br> WPA+PSK WPA+AES-CCM<br> 11<br><br>Current Clients: <br><br><br>

答案 1 :(得分:1)

你可以试试这种方式

&#13;
&#13;
var input = "BSSID:00:00:00:00:00:00<br>Encryption: WPA+PSK WPA+AES-CCM<br>Channel: 11<br><br>Current Clients: <br><br><br>";
var items = input.split("<br>").filter( function(item){ return item.indexOf( ":" ) != -1 });

//console.log(items);

var map = {};
items.forEach( function( item ){
  var key = item.substring( 0, item.indexOf( ":") );
  var value = item.substring( item.indexOf( ":") + 1);
  map[ key ] = value;
})

//console.log( map );

console.log( map.BSSID ); //value of BSSID
console.log( map.Encryption ); //value of Encryption
&#13;
&#13;
&#13;