How do I get text between these two elements, for example:
a) texto <br /> texto texto texto <br />
I would like to take what is between a)
and <br />
of the end, only the end:
"texto <br /> texto texto texto"
How do I get text between these two elements, for example:
a) texto <br /> texto texto texto <br />
I would like to take what is between a)
and <br />
of the end, only the end:
"texto <br /> texto texto texto"
Maybe not the best answer, but it works the way you want it to. I used this Regex below:
a\)\s.*(?=\s<)
Will return:
a) texto <br /> texto texto texto
Then I make a replace
in a)
, getting only:
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>Textooriginal:<br/><textareaid="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>
It's much easier to remove what you do not want:
^a\)\s*
at start, or \s*<br\s*/?>\s*$
at the end. Then:
^a\)\s*|\s*<br\s*/?>\s*$
Using the replace()
function with the flags global
and ignorecase
.
Or, if there are multiple <br>
at the end:
^a\)\s*|\s*(?:<br\s*/?>\s*)+$
Being that you are using ASP classic:
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)
Since no one has yet posted a response on how to do this in JavaScript , I'll be leaving my answer here too if 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
/a\)(.*)<br\s?\/>/
a\)
- Search for a)
(.*)
- Make a catch group with all terms found ( .*
) except \n
, which can be captured with [^]
if necessary (or [\S\s]
, if not working with JavaScript). <br\s?\/>
- Search for <br/>
or <br />
When using /(.*)/
as a regular expression, the compiler will create a capture group, which will be the place where text will be saved. So we can put texts before and after to limit the capture.
To return the text of an expression, use:
/exemplo/.exec(texto)[0]
Instead of 0
, you can swap for the created catch group. In your regex, for example, the number 1
is used to reference the group.
The String#trim()
method was used to remove the start and end spaces. (optional)