B
B
baroman4ik2021-11-29 13:02:27
JavaScript
baroman4ik, 2021-11-29 13:02:27

How to correctly output a string in jsx with regex content?

I tried wrapping the text matching the regex condition in a tag and displaying it in but if I use react-html-parser it outputs [object Object] when I pass text.replace(/#([^ ]+)/g, ReactHtmlParser(#$1)) .
If I pass it without ReactHtmlParser, then it returns span not as a tag with regex content, but as a string. How to return span as tag with regex content? Thanks in advance)
The full code looks like this:

function TextArea({text, note, setText}) {
  return (
      <div className="note-container">
        <textarea 
          className="note-body" 
          onChange={e => {
            setText(e.target.value)
          }}
          placeholder='Choose this text...'
          rows={10}
          defaultValue={note.text || ''}
        > 
        </textarea>
        <p className="note-body">{text.replace(/#([^ ]+)/g, ReactHtmlParser(<span class="hashtag">#$1</span>)) || ''}</p>
      </div>
  )

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Everybody_Lies, 2021-11-29
@Everybody_Lies

first replace in the text, and then pass this string to the ReactHtmlParser function
Example

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question