Reputation: 4625
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
},
...
]
Upvotes: 0
Views: 89
Reputation: 485
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": "? "
}
]
Upvotes: 1
Reputation: 485
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)]?"
}
]
Upvotes: 1