How do I marry this range of elements?

Asked

Viewed 158 times

1

How do I pick up a text between these two elements, for example:

a) texto <br /> texto texto texto <br />

I’d like to take what’s between a) and the <br /> of the end, only the end:

"texto <br /> texto texto texto"

https://regexr.com/3hdei

  • What would be the result after discarding?

  • text <br /> text text text

3 answers

2

It’s much easier to remove what you don’t want:

  • ^a\)\s* at the beginning, or
  • \s*<br\s*/?>\s*$ in the end.

So:

^a\)\s*|\s*<br\s*/?>\s*$

using the function replace() with the flags global and ignorecase.


Or, if there are several <br> in the end:

^a\)\s*|\s*(?:<br\s*/?>\s*)+$


Being that it is using classic ASP:

Dim texto : texto = "a) texto <br /> texto texto texto <br />"

'RegEx
Set re = New RegExp
re.Global = true
re.IgnoreCase = true
re.Pattern = "^a\)\s*|\s*(?:<br\s*/?>\s*)+$"

texto = re.Replace(texto, "")

Set re = Nothing

Response.Write(texto)
  • I don’t know if it was you who gave me that expression https://regexr.com/3qbc3 some time ago, it was?

2

Since no one has yet published an answer on how to do this in Javascript pure, I’ll be leaving my answer here too in case anyone needs it.

var txt = "a) texto <br /> texto texto texto <br />";
/a\)(.*)<br\s?\/>/.exec(txt)[1].trim(); // texto <br /> texto texto texto

Or

var txt = "a) texto <br /> texto texto texto <br />";
var rgx = /a\)(.*)<br\s?\/>/;
var res = rgx.exec(txt)[1].trim(); // texto <br /> texto texto texto

Explanation


Regex

/a\)(.*)<br\s?\/>/
  • a\) - Looking for a)
  • (.*) - Make a capture group with all found terms (.*) except for the \n, that can be captured with [^] if necessary (or [\S\s], if you are not working with Javascript).
  • <br\s?\/> - Looking for <br/> or <br />

Javascript

When using /(.*)/ as a regular expression, the compiler will create a capture group, which is the place where text will be stored. So we can put texts before and after to limit the capture.
To return the text of an expression, use:

/exemplo/.exec(texto)[0]

In place of 0, you can switch to the created capture group. In your regex, for example, the number is used 1 to reference the group.

Already the method String#trim() was used to remove the beginning and end spaces. (optional)

  • 1

    Good answer +1. A detail: recommend using [\s\S] instead of [^] to avoid compatibility problems

  • 1

    Truth right... The [^] is more compact, and also takes less time to compile, but I forgot that detail. Good, I’ll add here!

1


Maybe not the best answer, but it works as you wish. I used this Regex below:

a\)\s.*(?=\s<)

Will return:

a) texto <br /> texto texto texto

Then I make a replace in the a), just staying:

texto <br /> texto texto texto

resultado = $('#texto1')
            .text()
            .match(/a\)\s.*(?=\s<)/)
            .toString()
            .replace('a) ','');

$('#texto2').text(resultado);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Texto original:
<br />
<textarea id="texto1" style="display: inline-block; width: 350px; height: 50px;">
Texto texto texto texto
a) texto <br /> texto texto texto <br />
b) texto <br /> texto texto texto <br />
</textarea>
<br />
Texto capturado:
<br />
<textarea id="texto2" style="display: inline-block; width: 350px; height: 20px; background: yellow;">
</textarea>

  • Does it work? Not error? Which browser are you using? Chrome 61 accuses that mistake

  • I had no idea. It was announced here and implemented (Issue 4545) on August 21 in Chrome 62.

  • Opera 49+ (status)

  • @That’s what it was. I just modified it). *(?= <br />) . Now how do I ask the question? I wanted an individual form. https://regexr.com/3he44 . The question will be the initial text that will always end with two <br /><br /> but in the middle can have formatting tags and other <br />.

  • @I thank you, I’m giving up, I came so close, but... your code works correctly in plain text, when I put it in a textarea it does not work error. To using to get the question: Set regx = New Regexp regx.Ignorecase = True regx.Global = True regx.Pattern = "(.| n)?(to).?)" Set Partestring = regx. Execute(questao) For Each Parte in Partestring pergunta=replace(Parte.Value,"a)","") Next Set regx = Nothing

  • @In other words, in the "questao"), a textarea fckeditor editor, the strange thing is that the answers work. Your code gives error in Retstr = Matches(0) when recovering with fckeditor.

  • @I take the whole value of the textarea and individual if the rules, one for question and the other for the answers, then play each one in a variable where I want to write in the bank.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.