0

i'm trying to break multiple strings on the \n entry inside a string. currently the code is as followed:

<p ng-if="controller.service.variable" class="card-text" ng-repeat="factor in controller.service.variable.replace('\n\n','\n').split('\n')">{{factor}}</p>

This works just fine when there are two \n\n inside the string, when theres more then two, the split breaks and no longer shows anything.

the \n\n replace was supposed to fix this in a earlier bug, where two \n\n would break the string aswell. any ideas?

Current test data being split:

gjdfbjgd\n\njffsjfsbf\n\nsfj \n\nfsbfsssfsfsf
8
  • you can use split with regex .split(/\n*/), also no need of replace Commented Jan 20, 2020 at 10:26
  • @AZ_Data is now showing up, but not breaking lines propperly like it should with \n Commented Jan 20, 2020 at 10:33
  • my bad it should be .split(/\n+/) Commented Jan 20, 2020 at 10:35
  • @AZ_Still seems to showing up with spaces instead of the intended \n enters Commented Jan 20, 2020 at 10:40
  • can you paste your data you are splitting? Commented Jan 20, 2020 at 10:43

2 Answers 2

1

turned out to be a simple fix. Added the CSS: White-space:pre-wrap which fixed it.

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

1 Comment

Everyone was busy trying to fix it programmatically all the while there was a simple css fix.
0

You can use String.prototype.split with the regex.

let str  =`gjdfbjgd

jffsjfsbf

sfj 




fsbfsssfsfsf`;
console.log(str.split(/\n+/))

If string includes \n

let str  =`gjdfbjgd\\n\\njffsjfsbf\\n\\nsfj\\n\\n\\n\\nfsbfsssfsfsf`;
console.log(str)
console.log(str.split(/[\\n]+/))

3 Comments

Giving this a try and right now it's return the following error with this expression: Lexer Error: Unexpected next character at columns 50-50 [] in expression [datagathering.wam.IDD_Technical_Factor__c.split(/[\n]+/].
When i remove one \n it will return the following error Syntax Error: Token '/' not a primary expression at column 49 of the expression [datagathering.wam.IDD_Technical_Factor__c.split(/[ ]+/)] starting at [/[ ]+/].
why you tried .split(/[\n]+/] not .split(/[\\n]+/]?

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.