Bacancy Technology
Bacancy Technology represents the connected world, offering innovative and customer-centric information technology experiences, enabling Enterprises, Associates and the Society to Rise™.
12+
Countries where we have happy customers
1050+
Agile enabled employees
06
World wide offices
12+
Years of Experience
05
Agile Coaches
14
Certified Scrum Masters
1000+
Clients projects
1458
Happy customers
Artificial Intelligence
Machine Learning
Salesforce
Microsoft
SAP
August 27, 2024
If you want to split the string into an array based on the pattern of a number followed by a dot and space, you can use a regular expression (regex) in JavaScript to achieve that.
Here’s an example of using regex to split the string and achieve the desired output:
const inputString = '1. Lorem Ipsum. 2. Lorem Ipsum. 3. Lorem Ipsum'; const outputArray = inputString.split(/\d+\.\s/).filter(item => item !== ''); console.log(outputArray); // Output: [ '1. Lorem Ipsum.', '2. Lorem Ipsum.', '3. Lorem Ipsum.' ]
split(/\d+\.\s/) uses a regular expression as the argument to split() instead of a simple string. /\d+\.\s/ is a regex pattern that matches one or more digits (\d+), followed by a dot (\.), and a space (\s).
The split() method separates the string into an array based on this regex pattern.
filter(item => item !== ”) is used to remove any empty strings from the resulting array, which might occur due to splitting the string.