1

I have simple concatenated string of Name, UserId & TaxId

"Tester-N-U Academic Development Center-[10703]-[27-2008258]"

How can ? break this into array of strings so that in array[0] is 'Tester-N-U Academic Development Center', in array[1] is 10703 and in array[2] is 27-2008258

Here's what I tried

const input = $scope.Name_UserId_TaxID;
const output = input.match(/[^[\]]+(?=\])/g);
const splitProviderName = $scope.selectedValue.Name_UserId_TaxID.split('-', 1);
$scope.selectedProviderName = splitProviderName[0];
$scope.selectedProviderID = output[0];
$scope.selectedTaxID = output[1];

But the problem here is I am only getting the word Tester I am expecting Tester-N-U Academic Development Center

6
  • You could use a regular expression, but the best solution would be to change the string format to provide the values separately. Commented Jul 31, 2019 at 13:51
  • Updated in the question Commented Jul 31, 2019 at 13:53
  • What delimiter do you want to use? Commented Jul 31, 2019 at 13:53
  • 2
    Try input.match(/(.*)-\[(.*)\]-\[(.*)\]/) Commented Jul 31, 2019 at 13:54
  • 1
    "Tester-N-U Academic Development Center-[10703]-[27-2008258]".replace(/]/g, '').split('-[') Commented Jul 31, 2019 at 13:54

2 Answers 2

2

You may use this match based on lookahead regex:

var str = 'Tester-N-U Academic Development Center-[10703]-[27-2008258]';

var arr = str.match(/(?<=\[)[\d-]+(?=\])|^.+?(?=-\[)/g);

console.log(arr);

RegEx Demo

RegEx Details:

  • (?<=\[): Assert that there is [ at previous position
  • [\d-]+: Match 1 or more of digit or hyphen characters
  • (?=\]): Assert that there is ] at next position
  • |: OR
  • ^.+?: Match 1 or of any characters (non-greedy)
  • (?=-\[): Assert that there is -[ at next position
Sign up to request clarification or add additional context in comments.

9 Comments

for regex expression
You can click on Run Code Snippet to see a live demo. I suspect you're running an older Javascript regex engine that may not support lookbehind. Can you try: /[\d-]+(?=\])|^.+?(?=-\[)/ as regex?
try to put array [1] getting undefined
This works thank you what if the name is like this 'Tester-N-U Academic Development Center~[10703]~[27-2008258]'; used ~ as separator
but I want to exclude brackets also for userId & TaxId
|
2

You may use

var s = "Tester-N-U Academic Development Center-[10703]-[27-2008258]";
var rx = /-\[([^\][]*)]/;
console.log(s.split(rx).filter(Boolean));

The regex matches

  • -\[ - -[
  • ([^\][]*) - captures into Group 1 any zero or more chars other than [ and ]
  • ] - a ] char.

Since the String#split returns all captured substrings, too, the value in Group 1 lands in the resulting array.

Note you do not need to use g as split looks for all matches in the input by default.

Note that .filter(Boolean) is required to remove any empty items from the result.

If you know there are only 3 pieces of text you need to get from the string you may use

/^(.*)-\[([^\][]*)]-\[([^\][]*)]$/

and grab Group 1, Group 2 and Group 3 values out of it. See this regex demo.

var s = "Tester-N-U Academic Development Center-[10703]-[27-2008258]";
var rx = /^(.*)-\[([^\][]*)]-\[([^\][]*)]$/;
var result = s.match(rx);
if (result) {
   console.log("scope.selectedProviderName =", result[1]);
   console.log("scope.selectedProviderID =", result[2]);
   console.log("scope.selectedTaxID =", result[3]);
}

ES6+:

var s = "Tester-N-U Academic Development Center-[10703]-[27-2008258]";
var rx = /^(.*)-\[([^\][]*)]-\[([^\][]*)]$/;
var [_,selectedProviderName,selectedProviderID,selectedTaxID] = s.match(rx);
console.log("scope.selectedProviderName =", selectedProviderName);
console.log("scope.selectedProviderID =", selectedProviderID);
console.log("scope.selectedTaxID =", selectedTaxID);

1 Comment

Where is the required jQuery? :-D

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.