1

This string has regex character classes which need to be removed. As well as reduce multiple spaces to single space.
I can chain replace() but thought to ask if one can suggest a one regex code to do the whole job at one go. How can it be done? Thanks

"\n\t\t\t \n\n\t \n\t \t\tFood and drinks \n \t\n"

This is needed:

"Food and drinks"

var newStr = oldStr.replace(/[\t\n ]+/g, '');  //<-- failed to do the job
2
  • Don't forget that replacing the spaces will replace all the spaces. Commented Aug 24, 2016 at 0:31
  • I wouldn't do it in one replace, although I think it is possible. Its much more clear to use one regex for removing chars, one for retaining single spaces, and one for trimming. Commented Aug 24, 2016 at 0:48

2 Answers 2

2

You want to remove all leading and trailing whitespace (space, tab, newline) but leave the spaces in the internal string. You can use the whitespace character class \s as shorthand, and match either the start or the end of the string.

var oldStr = "\n\t\t\t \n\n\t \n\t \t\tFood and drinks \n \t\n";

// ^\s+ => match one or more whitespace characters at the start of the string
// \s+$ => match one or more whitespace characters at the end of the string
// | => match either of these subpatterns
// /g => global i.e every match (at the start *and* at the end)

var newStr = oldStr.replace(/^\s+|\s$/g/, '');

If you also want to reduce the internal spaces to a single space, I would recommend using two regexes and chaining them:

var oldStr = "\n\t\t\t \n\n\t \n\t \t\tFood   and      drinks \n \t\n";
var newStr = oldStr.replace(/^\s+|\s+$/g, '').replace(/\s+/g, ' ');

After the first .replace() all of the leading and trailing whitespace is removed, leaving only the internal spaces. Replace runs of one or more space/tab/newline with a single space.

One other way to go could be to reduce all runs of whitespace to a single space, then trim the one remaining leading and trailing space:

var oldStr = "\n\t\t\t \n\n\t \n\t \t\tFood   and      drinks \n \t\n";

var newStr = oldStr.replace(/\s+/g, ' ').trim();
// or reversed
var newStr = oldStr.trim().replace(/\s+/g, ' ');

.trim() doesn't exist prior to ES5.1 (ECMA-262) but the polyfill is essentially .replace(/^\s+|\s+$/g, '') (with a couple of other characters added) anyway.

Sign up to request clarification or add additional context in comments.

Comments

2

I'd recommend this pattern (assuming you want to keep \ns or \ts in your main string):

/^[\t\n ]+|[\t\n ]+$/g

If you don't want to keep them, you can use something like this:

/^[\t\n ]+|[\t\n]*|[\t\n ]+$/g

2 Comments

I tried the last pattern since I don't want to keep them, but it leaves a new line at the end. Try it in the console and you should see.
Trying your first removed the leading but left the trailing spaces, because it needs the global option to get both: oldStr.replace(/^[\t\n ]+|[\t\n ]+$/g, '');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.