0

Input: Hi #[Pill(google.0.LastName)], How is going #[Pill(google.0.FirstName)]?

this Input has regex expression of const pillRegex = /#\[dataPill\((.*)\)\]/i;

#[Pill(google.0.LastName)] and #[Pill(google.0.LastName)] are the matching results.

I want to build an array with objects and indicate if the value is matched or not. (If you have better data structure, you can use it)

[
   {
      pill: false, //not matched
      svalue: 'Hi ' //value
   }, {
      pill: true, //matching regex
      svalue: ["google", "0", "Last Name"]] //the value

   },
   ...
]
merry-go-round
  • 4,533
  • 10
  • 54
  • 102
  • I think you’re going to need to explain yourself a little better, because I have no idea what you want. – Robert Clarke Jul 29 '20 at 21:39
  • Ok I'm editing it now – merry-go-round Jul 29 '20 at 21:39
  • @RobertClarke Did it help? – merry-go-round Jul 29 '20 at 21:43
  • Okay, I think I see what you mean. I guess you could split the string on each space character, so you have each word on its own in an array. Then you could loop through the array and check each string, to see if it matches the regex. Then push the relevant data to the output array as your iterate through the array of words. Hopefully that makes some sense – Robert Clarke Jul 29 '20 at 21:48
  • Could you provide an example code? "push the relevant data to the array as your iterate through the array" <- This part doesn't make sense but it will be easy to verify it with code – merry-go-round Jul 29 '20 at 21:50

2 Answers2

1

Okay here is some code I wrote, that I think achieves what you’re looking for:

var input = “Hi #[Pill(google.0.LastName)], How is going #[Pill(google.0.FirstName)]?”;

const regex = new RegExp(/#\[Pill\(google\.\d\.[a-zA-Z0-9]+\)\]/);

input = input.split(“ “);

output = [];

input.forEach(word => {
    temp = {
        pill: regex.test(word),
        svalue: word + “ “
    };
    output.push(temp);
});

console.dir(output);

Output:

[
  {
    "pill": false,
    "svalue": "Hi"
  },
  {
    "pill": true,
    "svalue": "#[Pill(google.0.LastName)],"
  },
  {
    "pill": false,
    "svalue": "How"
  },
  {
    "pill": false,
    "svalue": "is"
  },
  {
    "pill": false,
    "svalue": "going"
  },
  {
    "pill": true,
    "svalue": "#[Pill(google.0.FirstName)]?"
  }
]
Robert Clarke
  • 475
  • 4
  • 14
1

Okay, here’s my second answer to your problem. This time I’m splitting it on the regex.

var input = “Hi #[Pill(google.0.LastName)], How is going #[Pill(google.0.FirstName)]?”;

const regex = new RegExp(/#\[Pill\([A-Za-z0-9]+\.\d\.[a-zA-Z0-9]+\)\]/);

input = input.split(/(#\[Pill\([A-Za-z0-9]+\.\d\.[a-zA-Z0-9]+\)\])/g);

output = [];

input.forEach(word => {
    temp = {
        pill: regex.test(word),
        svalue: word + “ “
    };
    output.push(temp);
});

console.dir(output);

Output:

[
  {
    "pill": false,
    "svalue": "Hi  "
  },
  {
    "pill": true,
    "svalue": "#[Pill(google.0.LastName)] "
  },
  {
    "pill": false,
    "svalue": ", How is going  "
  },
  {
    "pill": true,
    "svalue": "#[Pill(google.0.FirstName)] "
  },
  {
    "pill": false,
    "svalue": "? "
  }
]
Robert Clarke
  • 475
  • 4
  • 14