0

i am having a problem with some regex in a javascript function My string looks like this...

[Space]SomeString[Space][Tab]SomeString[Space][Tab][LineBreak]

SomeString[LineBreak]

[Space]SomeString[Space][Tab]SomeString[Space][Tab][LineBreak]

SomeString[LineBreak]

I want to remove the [Tab][LineBreak] but keep the [LineBreak] so my output would be

[Space]SomeString[Space][Tab]SomeString[Space]SomeString[LineBreak]

[Space]SomeString[Space][Tab]SomeString[Space]SomeString[LineBreak]

I have tried:

value.replace(/\t\n/g, '');

but that didn't work i also tried:

value.replace(/\s+/g, '');

but that removed all the line breaks

Can anyone help please? Thanks

1
  • I finally solved it thank goodness!! You're not gonna believe this but the code was fine all along. I was echoing the script in php and it was converting the /t and /n into tabs and breaks in my source, so no wonder it didnt work. hahaha. Thanks for all the help tho guys. Commented May 14, 2013 at 15:19

1 Answer 1

2

This will do the trick

str.replace(/\t(\r\n|\r|\n)/g,'');

Here is a demo fiddle.

Edit:

str = str.replace(/^\s|\t([\r\n]+)|([\r\n]+)\t|\s$/g,'');

Here is the "upgraded" example

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

4 Comments

That will remove both the tab AND the line break.
That's right, tab+break is removed, but a break that is not preceded by tab will not.
i updated the fiddle jsfiddle.net/9fC4K/3 with my exact string but used x's and o's. The fiddle works, but mine still doesnt?
jsfiddle.net/9fC4K/6 I used str.replace(/^\s|\t([\r\n]+)|([\r\n]+)\t|\s$/g,'$2'); So... ^\s : the string begins with a whitespace; \t([\r\n]+) : the string contain a TAB followed by line break ; ([\r\n]+)((\t)) : the string contains a line break followed by a TAB ; \s$ : the string ends with a whitespace. When any of these conditions matches then the matched content is removed.

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.