search

Home  >  Q&A  >  body text

javascript - How to determine if the string returned by the background contains a newline?

Just like the title.

Data returned by the background:
Data displayed in the interface document:

Data from the console on chrome:

(I randomly printed these two pieces of data, just to show that there are line breaks in the background)

The final display
is a one-line display without a line break effect and requires front-end processing

I first check whether it contains a newline character
for (let i = 0; i < response.data.Data.length; i ) {

        if (response.data.Data[i].content.indexOf('\n') >= 0) {
          console.log(i)
        }
      }

The data in the printed index does not have a newline character, which is inconsistent.
Is this correct? Or do I need to change the conditions to judge?
Please give me some advice~

某草草某草草2819 days ago1036

reply all(3)I'll reply

  • 習慣沉默

    習慣沉默2017-05-19 10:40:15

    I don’t understand why I wrote this loop to traverse. The result returned to you is response.data.content. You can use regular expressions to do it

    /\n/.test(response.data.content)

    reply
    0
  • 高洛峰

    高洛峰2017-05-19 10:40:15

    You have console.log ↵, of course you have to write indexOf('↵')

    reply
    0
  • 世界只因有你

    世界只因有你2017-05-19 10:40:15

    Problem Solved

    The original tag that wrapped the returned content was <p>, but later I roughly changed it to <pre>. The h5 tag
    can be displayed in new lines
    But there is a problem. If other content without line breaks exceeds the width, It will exceed the display and will not wrap automatically

    Added css control at the end
    pre{
    white-space: pre-wrap;
    word-wrap: break-word;
    }

    It’s solved

    reply
    0
  • Cancelreply