Home > Enterprise >  Split a string in two, keeping words intact and returning them
Split a string in two, keeping words intact and returning them

Time:08-16

`As the title says, looking to see how I can split a string into two parts, while keeping full words intact, and having access to both parts with JavaScript. I am using this string in a pug file.

For instance, I have a very long string

let str = `hello i am a very long string that needs to be split into two different parts! thank you so much for helping.'

I want the first 70 characters, to use as a headline, then the rest to use for the body of the element.

I have used this regex that I found on an older post that gives me the first 70 characters, with full words...

str.replace(/^([\s\S]{70}[^\s]*)[\s\S]*/, "$1")

which does what I want it to do! But now I need to figure out if there is a way to get the rest of that string?

str1 = `hello i am a very long string that needs to be split into two different`
str2 = `parts! thank you so much for helping.`

Again, using this in a pug file, so if there is a simple answer, I would love that. Otherwise I suppose I could just write a function and import the script to the pug view.

CodePudding user response:

After extracting the first part of the string using your method you can use the length of str1 to determine where in str the second part begins. From there you can use substring to extract it.

let str = `hello i am a very long string that needs to be split into two different parts! thank you so much for helping.`;
let str1 = str.replace(/^([\s\S]{70}[^\s]*)[\s\S]*/, "$1");
let str2 = str.substring(str1.length).trimStart();

Using trimStart you can to remove any whitespace that would appear at the start of str2.

CodePudding user response:

What would you want to do when the split happens on a whitespace character. Say; the space right after 'different'. It would mean that the part prior to the space is 69 characters, but 70 if you'd include the space. Assuming you want to capture the next word for good measure, try to utilize JS's zero-width lookbehind:

(?<=^.{70}\S*)\s 

See an online demo


  • (?<= - Open positive lookbehind;
    • ^.{70} - 70 Characters right after start-line anchor;
    • \S* - 0 Non-whitespace chars;
  • \s - Capture 1 whitespace characters to split on.

const str = 'hello i am a very long string that needs to be split into two different parts! thank you so much for helping';

const result = str.split(/(?<=^.{70}(?:\S)*)\s /);

console.log(result);

CodePudding user response:

If you instead just take a look at the matches:

const matches = str.match(/^([\s\S]{70}[^\s]*)[\s\S](.*)/);

You will see the second match contains your header text and the third match your body text.

CodePudding user response:

An alternative solution to the previous one, but using only regex would be the following:

let listStrings = str.split(/^([\s\S]{70}[^\s]*)[\s\S](.*)/)).filter(Boolean)

I added to your regex the last part (.*) to obtain the rest of the string as another group. Then, using the javascript string split function you can apply the regex and get a list of strings, being those the groups obtained by the regex.

The .filter(Boolean) is used to remove the empty strings obtained in the response:

0:""
1:"hello i am a very long string that needs to be split into two different"
2:"parts! thank you so much for helping."
3:""
  • Related