-1

I'm trying to parse

Mode:Managed  Frequency:2.462 GHz  Access Point: 00:00:00:00:00:00 

using javascript regex.

My goal is to obtain something like this:

{
    Mode: 'Managed',
    Frequency: '2.462 Ghz',
    Access Point: ''
}

I tried something like this :

((Mode\s*):\s*)(\w+) 

but I obtain 3 results: Mode: Managed, Mode: and Mode. Any idea ?

mplungjan
  • 169,008
  • 28
  • 173
  • 236
auk
  • 181
  • 4
  • 10
  • Post your code. Which function did you used ? – Rahul May 04 '17 at 16:30
  • Post an example of the text you want to parse (2 or 3 lines at least) along with the expected output. – ibrahim mahrir May 04 '17 at 16:33
  • You have only one line in the example – mplungjan May 04 '17 at 16:39
  • Exact duplicate of [What is the purpose of the passive (non-capturing) group in a Javascript regex?](http://stackoverflow.com/questions/18578714/what-is-the-purpose-of-the-passive-non-capturing-group-in-a-javascript-regex) –  May 04 '17 at 16:47
  • [*Some people, when confronted with a problem, think “I know, I'll use regular expressions.” Now they have two problems.* - Jamie Zawinski](http://regex.info/blog/2006-09-15/247) –  May 04 '17 at 16:48
  • @JarrodRoberson The duplicate link is useless. And this can be done using RegExp :3. – ibrahim mahrir May 04 '17 at 16:49

4 Answers4

1

var str = "\
Mode:Managed  Frequency:2.462 GHz  Access Point: 00:00:00:00:00:00\n\
Mode:UnManaged  Frequency:3.462 GHz  Access Point: 00:00:00:00:00:00\n\
Mode:UnUnManaged  Frequency:4.462 GHz  Access Point: 00:00:00:00:00:00\
";

// look for some characters between "Mode:" and "Frequency:" group them as the first group
// look for some characters between "Frequency:" and "Access Point:" group them as the second group
// look for some characters after "Access Point:" group them as the third group
var regex = /Mode:\s*(.+)\s+Frequency:\s*(.+)\s+Access Point:\s*(.+)/gm;

var r, res = [];
while(r = regex.exec(str)) {
    res.push({
        "Mode": r[1].trim(),            // the first group is the Mode (trim to remove an leading or trailing spaces) 
        "Frequency": r[2].trim(),       // ...
        "Access Point": r[3].trim()     // ...
    });
}

console.log(res);
ibrahim mahrir
  • 31,174
  • 5
  • 48
  • 73
0

Try with following regex.

Regex: (Mode:\S*)\s*(Frequency:\S*\s*GHz)\s*(Access Point:\s*\S*)

Regex101 Demo

Rahul
  • 2,658
  • 12
  • 28
0

but I obtain 3 results: Mode: Managed, Mode: and Mode. Any idea ?

You have 3 capturing groups so you capture three things.

Pang
  • 9,564
  • 146
  • 81
  • 122
0

Text

"Mode:Managed  Frequency:2.462 GHz  Access Point: 00:00:00:00:00:00  "

Regex:

(.*:.*)\ {2}

Result:

  1. Mode:Managed
  2. Frequency:2.462 GHz
  3. Access Point: 00:00:00:00:00:00
Community
  • 1
  • 1
Siamand
  • 1,080
  • 10
  • 19