How to split a string in javascript based on this specific scenario

user1030181

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

anubhava

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to split a string by dash at specific position JavaScript

how to split a string based on value in javascript

How to split String based on specific character without using strok

How to split up a string in an array based on a specific marker

how to split excel sheet based on string values in a specific column

Split HTML string into sections based on specific tag?

Split a string based on specific substring or pattern in pandas

How to split a string javascript

How to split string into specific keywords?

How to split string by specific keywords?

How split this specific string into array

How to split a specific string in Java

How to split a string at a specific point?

How to split a string based on a char

Javascript: split string into strings of specific length

Javascript with Regex to split string at different specific characters

R: How to split a specific column based on symbol?

Python: How to split a list based on a specific element

How to split a list of strings based on delimiter string that ends with specific character in Python?

How to split string based on position in string

Split string based on the first occurring number javascript

split a string in javascript based on start and end delimiters

Split a string into a list of tuples based selectively on specific commas within the string

How to split string with character \ - Javascript

How to split string in JavaScript with splitter?

ReactJS - Rendering a specific component based on scenario

How to split a dataframe based on specific texts of specific column

How to assign to variables to a split string with specific indeces?

How to string split, match, and output a specific pattern?