I'm looking for an answer to trim a string to a certain length of chars without cutting words. I searched the questions and found this:
javascript shorten string without cutting words
I wanted to use @Hamish s answer with the regex replacement but encountered problems with multiline texts.
@Hamish s answer:
"this is a longish string of test".replace(/^(.{11}[^\s]*).*/, "$1");
//"this is a longish"
I searched for similar questions and found out that the dot '.' does not include newlines \n. Normally one could end an 's' at the end to have the dot also matching newlines, but in javascript that's obviously not working. I read in other threads that I should use [\s\S] to match any character. So I tried using @Hamish s regex expression like this:
infotext = infotext.replace(/^([\s\S]*{10}[^\s]*).*/, "$1");
But then I get an error message which says:
Uncaught SyntaxError:
Invalid regular expression: /^(\[\s\S\]*{10}[^\s]*).*/: Nothing to repeat.
Can somebody help me out with that. I really can't find a solution to match any character... Thx in advance. M
You can try:
infotext = infotext.replace(/^([\s\S]{10}\S*)[\s\S]*/, "$1");
Problem is your use of [\s\S]*{10}