Nalawala Murtaza
Nalawala Murtaza

Reputation: 4810

Matches exactly regular expression not work in dart

Java script regular expression

/\d(.*)\s[0-9A-Z:]{17}/i

Test Case

console.log("11  salus_biura                      68:d7:9a:da:7d:6a   WPA2PSK/AES            29       11b/g/n NONE   In  NO    ".match(/\d(.*)\s[0-9A-Z:]{17}/i)[1]);

Dart code

void main() {
   String wifiInfo = "11  salus_biura                      68:d7:9a:da:7d:6a   WPA2PSK/AES            29       11b/g/n NONE   In  NO    ";
   Match? matches =   RegExp(r'\\d(.*)\\s[0-9A-Z:]{17}', caseSensitive: true).firstMatch(wifiInfo);
   print(matches?.group(1)); // null 
} 

issue {17} Matches exactly not work

output in dart 1 salus_biura

Upvotes: 1

Views: 162

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626748

In your Dart code,

  • Use caseSensitive: true as you have an i flag (that enables case insensitive matching) in your JavaScript regex
  • Use single backslashes in a raw string literal. "\\d" = r"\d".

The fix code can look like

void main() {
  String wifiInfo = "11  salus_biura                      68:d7:9a:da:7d:6a   WPA2PSK/AES            29       11b/g/n NONE   In  NO    ";
  Match? matches =   RegExp(r'\d(.*)\s[0-9A-Z:]{17}', caseSensitive: false).firstMatch(wifiInfo);
  print(matches?.group(1)); // => 1  salus_biura   
}

Upvotes: 3

Related Questions