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
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 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 positionCollected from the Internet
Please contact [email protected] to delete if infringement.
Comments