Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

303
Vistas
Less or greater than characters except html tags?

I need to replace less or greater than(< >) characters, but keep any html tags(simple tags will do like <b>text</b> without arguments). So the input below:

<b>> 0.5 < 0.4</b> - <>

Should be:

<b>&gt; 0.5 &lt; 0.4</b> - &lt;&gt;

All I managed to find and edit now is this expr:

<\/?[a-z][a-z0-9]*[^>\s]*>|([<>])

It groups the < and > characters but it also matches tags witch I don't need to replace

UPD: Thanks to @Sree Kumar, here's the final functions:

String.prototype.replaceAt = function (index, char) {
    let arr = this.split('');
    arr[index] = char;
    return arr.join('');
};

String.prototype.escape = function () {
    let p = /(?:<[a-zA-Z]+>)|(?:<\/[a-zA-Z]+>)|(?<lt><)|(?<gt>>)/g,
        result = this,
        match = p.exec(result);

    while (match !== null) {
        if (match.groups.lt !== undefined) {
            result = result.replaceAt(match.index, '&lt;');
        }
        else if (match.groups.gt !== undefined) {
            result = result.replaceAt(match.index, '&gt;');
        }
        match = p.exec(result);
    }
    return result;
};
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Try this regex:

<(?!\/?\w+>)|(?<!<\w+|<\/\w+)>

Explanation

  • <(?!\/?\w+>) finds all '<' symbols (except in tags)
  • (?<!<\w+|<\/\w+)> finds all '>' symbols (except in tags)

You can use them separately:

let str = '<b>> 0.5 < 0.4</b> - <>';
let lessThen = /<(?!\/?\w+>)/g;
let greaterThen = /(?<!<\w+|<\/\w+)>/g;

str = str.replace(lessThen, '&lt;');
str = str.replace(greaterThen, '&gt;');

console.log(str); // <b>&gt; 0.5 &lt; 0.4</b> - &lt;&gt;

NB! It only finds symbols '<' and '>' between tags. It doesn't check that html is valid. For text like that <a></b> it will not find any matches.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Here is a way to do it using named groups. That is, name your desired group and look for it. It may be null or undefined at times because it didn't match. Hence, you will have to add the null check.

Notice (?<B>...) surrounding the "desired" group. Also, notice the null check in the 5th line.

let p = /(?:<[a-zA-Z]+>)|(?:<\/[a-zA-Z]+>)|(?<B>[<>])/g
let input = '<b>> 0.5 < 0.4</b> - <>';
let match = p.exec( input );
while( match !== null) {
    if( match.groups.B !== undefined ) console.log( match.groups.B );
    match = p.exec( input )
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda