0

How do I get given value?

name // google
index // 0
LastName //Last Name

My approach is not working because I have a singular condition '.'

value = '#[pill(google.0.LastName)]'
const [name, index, name] = value.split('.')

Regex is /#\[pill\((.*?)\)\]/gi

3
  • Regexp find the text inside () before splitting on a . Commented Aug 12, 2020 at 18:45
  • can you provide an example in the answer? I added Regex pattern Commented Aug 12, 2020 at 18:47
  • yeap; added the variable before that Commented Aug 12, 2020 at 18:48

3 Answers 3

2

var test = '#[pill(google.0.LastName)]';

console.log(
  test.substring(test.indexOf('(') + 1, test.indexOf(')')).split('.')
);

console.log(
  test.match(/\(([^)]+)\)/)[1].split('.')
);

Here are a couple different ways; one with substring and one with a regexp.

Sign up to request clarification or add additional context in comments.

Comments

1

You can forgo the split using three matching expressions in your regex like this , #\[pill\((.*)\.(.*)\.(.*)\)\]

var string = "#[pill(google.0.LastName)]"
var [_, name, Index, LastName] = string.match(/#\[pill\((.*)\.(.*)\.(.*)\)\]/)

console.log(name, Index, LastName)

1 Comment

can you provide a working example? It doesn't work on my end
1

You could split the parts of the matched string.

const
    string = '#[pill(google.0.LastName)]',
    [firstName, index, lastName] = string.match(/[^\(]+(?=\))/)[0].split('.');
    

console.log(firstName);
console.log(index);
console.log(lastName);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.