Extract certain text from an array value


I have an array which is the result of a wifi scan. each element of the list contains the BSSID and Signal level the surrounding AP’s.


    0: "BSSID: b0:c5:54:dd:e1:20 Signal: -30"
    1 "BSSID: b0:c5:54:dd:e1:20 Signal: -40"
    2 "BSSID: b0:c5:54:dd:e1:20 Signal: -50"
    3 "BSSID: b0:c5:54:dd:e1:20 Signal: -60"

How can I extract the BSSID and signal level? I know that I have to use a regex but I have never worked before with regex. Is anyone outthere good with regex that can give me some help with it please?

RegexOne is a great resource for learning / referencing Regular Expressions.

Hi @Cacoomba, just a thought… since those strings all have fixed lengths and structure, couldn’t you simply take the substrings at the given positions? Something like

const data = scanArray.map(str => {
  return {
    bssid: str.substr(7, 17),
    signal: str.substr(33)

A regular expression would certainly be the safer approach, though. :-)

Hi m3g4p0p,

What you suggest its really smart. I’ll keep tha in mind if I dont end up with a regex.

Thanks, but I’d rather call it lazy. ^^ Anyway, here’s a cleaner solution with regexps:

const data = scanArray.map(str => {
  return {
    bssid: str.match(/BSSID:\s(\S*)/)[1],
    signal: str.match(/Signal:\s(\S*)/)[1]

We match "BSSID: " and "Signal: " respectively, followed by any sequence of non-whitespace characters. The return values are arrays where the first element is the whole match, and the second element the part within the (capturing parentheses), which is what we want (if there were more capturing parentheses they would be third, fourth etc.).

1 Like

Thanks m3g4p0p. That’s exactly what I was looking for.
I really appreciate it.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.