-2

This is my Regex pattern.

const dataPillRegex = /#\[dataPill\((.*)\)\]/g;

with value (Singular match), it prints the right array, But with value2 (multiple matches), it returns a wrong result.

value = "Hi #[dataPill(salesforce.0.LastName)], ?"
const splitedValue2 = value2.split(dataPillRegex); //['Hi ', 'salesforce.0.LastName', ', ?']

value2 = "Hi #[dataPill(salesforce.0.LastName)], How is going #[dataPill(salesforce.0.FirstName)]?"
const splitedValue2 = value.split(dataPillRegex); 
//['Hi ', 'salesforce.0.LastName)], How is going #[dataPill(salesforce.0.FirstName', ', ?']

splitedValue2 should be...

//['Hi ', 'salesforce.0.LastName', ', How is going ', 'salesforce.0.FirstName', ', ?']

Furthermore,,, It will be awesome if I can have want to have,,,

[
    'Hi ',
    '#[dataPill(salesforce.0.LastName)]',
    ', How is going ',
    '#[dataPill(salesforce.0.FirstName)]',
    '?'
]
merry-go-round
  • 4,533
  • 10
  • 54
  • 102

1 Answers1

0

Here’s my answer, from your duplicate question.

var input = ”Hi #[dataPill(salesforce.0.LastName)], ?”;

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

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

output = [];

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

console.dir(output);

Output:

[
  {
    "value": "Hi  ",
    "matched": false
  },
  {
    "value": "#[dataPill(salesforce.0.LastName)] ",
    "matched": true
  },
  {
    "value": ", ? ",
    "matched": false
  }
]
Robert Clarke
  • 475
  • 4
  • 14
  • Accepted. Can you work on this tooo? https://stackoverflow.com/questions/63164192/how-to-get-matching-regex-content-in-javascript – merry-go-round Jul 29 '20 at 23:58