How to capture textarea tag with new line?

10

How to get the values of a <textarea> with Regex, including new line?

I have the following expression to get a textarea :

([<]textarea.*[<\/textarea][>])

Example online.

The problem is that if textarea has a line break, the expression can not catch it.

From here my question: How to capture textarea with new line?

    
asked by anonymous 02.08.2016 / 17:39

4 answers

11

The problem with this REGEX is that by default . does not include \n , this way it would have to get around this fault, it could be with deny [^...] , which captures anything that is not in the group. p>

For your needs you can do this: <(textarea)([^>]*)>([^%]*?)</> .

See working at REGEX101

Explanation

  • <(textarea) - literally captures < and generates a literal group with textarea , which will be used as a shortcut.
  • ([^>]*)> - will be all attributes of the tag, remembering that attributes do not have > so I used his negation to get everything, finally it should end with the term of tag > .
  • ([^%]*?) - here is content to be captured, I have used the % deny since I do not want to have this in the middle, but if you have just change to another character, for example ¬ , remembering that because it is Deny includes any and all characters that are not in the group including \n .
  • </> - finally must catch the end of the tag. which was resumed with the shortcut of the group 1 .

Addendum

You can also use the s flag to allow . (Dot) to capture \n . changing REGEX to <(textarea)([^>]*)>(.*?)</> .

Remembering that% s of% should apply.

Example JS

string.match(/<(textarea)([^>]*)>(.*?)<\/>/gs); // aqui  foi necessário escapar o '/', para não ser interpretado como fim da REGEX '<\/>'.

See working at REGEX101

    
02.08.2016 / 17:52
9

You can do this:

(<textarea[\s\S]+?textarea>)

Example: link

The important part is [\s\S]+? , which basically allows everything , one or more times, and ? says to be lazy and achieve capture in the first opportunity you encounter. >     

02.08.2016 / 18:00
5

The other answers are correct and excellent, I just made some changes:

  • I've modified the @Sergio example to:

    /(<textarea[\s\S]+?<\/textarea>)/g

    Test: link

    This to avoid things like <textarea>abc<textarea> (see that the bar is missing, but in the original @Sergio regex I was getting the match)

  • If you need to match attributes and content separately, do so:

    /(<textarea([^>]+)>([\s\S]+?|)<\/textarea>)/g

  • I've modified the @GuilhermeLautert example to:

    /<(textarea)([^>]+)>([^%]*?)<\/>/g

    Test: link

    The answer works perfectly, but if you need to use / it will not work well due to </> , of course the situation varies in different languages, this is just for a specific situation.

  

Note: I came up with an example, but my knowledge was more limited, however follow the regex:

<textarea([^>]+)[>]([^<]+[^t]+[^e]+[^x]+[^t]+[^a]+[^r]+[^e]+[^a]+[^>]|.*)<\/textarea>
     

Result: link

     

However the other answers show better and simpler ways, this is just an alternative to study

    
02.08.2016 / 18:17
4

Hello friend, I was able to do this:

<textarea\b[^>]*>((\n*|.)*)<\/textarea>

Example: link

Explanation:

  • <textarea\b[^>]*> Capture the first tag, the \b border ensures that the tag matches <textarea . [^>]* matches all characters except > preventing the tag from containing two >>
  • ((\n*|.)*)) Captures the Group of the tag content. Capture any line break \n* or | catch all characters .*
  • <\/textarea> Ends with capture of closing tag
02.08.2016 / 18:37