Regex in javascript that selects something that on its sides (left, right or both) does not have certain terms

2

I am trying to find a regular expression that selects me those spaces in whites that on its left side, right side or both have any word except a specific word.

The regex must obtain the following result:

Internal spaces that you should not find

  • specify specific
  • other specific
  • specify another

Internal spaces you should find

  • another one

However, until now the best result I have achieved is this: RegExr

Edition: I failed to clarify that the accents are irrelevant. The objective is to suppress all the blank spaces, except those that have on any side (or both sides) the specific word. Another way to see the problem is to delete all existing spaces but the word specifies to keep the spaces it has on both sides.

    
asked by cristiandev05 13.10.2016 в 03:32
source

2 answers

1

Solution

/\b(?!especifica\b)(\w+\b)(\s+)\b(?!especifica\b)/gi


Description

The regular expression consumes the word before space (and the capture in the first group), and then captures the blanks within group 2 ( grupo[2] ).

/
\b                  # límite de palabra completa (antes de la 1er palabra)
(?!especifica\b)    # aserción (negative lookahead) para ver que no sea "especifica"
(\w+\b)             # consume la primer palabra
(\s+)               # coincide con los espacios en blanco buscados
\b                  # límite de palabra completa (antes de la 2da palabra)
(?!especifica\b)    # aserción para ver que la 2da palabra no sea "especifica"
/gi                 # g: todas las coincidencias;  i: no importa mayúsculas/minúsculas

Next, we'll use the starting position from where the match started, and the length of the first word ( grupo[1] ) to determine where the blank starts.


Code

var regex = /\b(?!especifica\b)(\w+\b)(\s+)\b(?!especifica\b)/gi;
var texto = 'especifica  especifica
             tres        especifica
             especifica  cuatro
             cinco       seis';

var grupo, posInicial, posFinal, resultado = "";

while ((grupo = regex.exec(texto)) !== null) {
    posInicial = grupo.index + grupo[1].length;
    posFinal = regex.lastIndex;
    resultado = resultado + "\n" 
              + 'Se encontró "' + grupo[2] 
              + '" desde la posición ' + posInicial 
              + ' hasta la posición ' + posFinal 
              + ' (después de la palabra "' + grupo[1] + '")';
}

document.getElementById("resultado").innerText = resultado;
<pre id="resultado"></pre>


Generate the regex dynamically

If you want to generate the regular expression dynamically, you have to make sure to escape any metacharacter that may be inside the specific word.

function generarExpresion(especifica) {
    especifica = especifica.replace(/[\/\^$*+?.()|[\]{}]/g, '\$&');
    return new RegExp("\b(?!" + especifica + "\b)(\w+\b)(\s+)\b(?!" + especifica + "\b)", "gi");
}

var regex = generarExpresion("especifica");


Remove (replace) blank spaces

Using replace, we can remove all blanks, except those that surround a specific word. For that, all matches (the first word and the space) are replaced by the value of the first group $1 (only the first word):

function juntarTodo() {
    var regex = /\b(?!especifica\b)(\w+\b)(\s+)\b(?!especifica\b)/gi;
    var texto = document.getElementById("area").value;
    var resultado = texto.replace(regex, "$1");
    document.getElementById("resultado").value = resultado;
}
<textarea id='area' rows="4" cols="60">especifica  especifica    uno        especifica  dos
   tres       especifica  cuatro        cinco       seis</textarea>
<button id='boton' onclick="juntarTodo()">Juntar Todo</button>
<textarea id='resultado' rows="4" cols="60"></textarea>

Demo in regex101

    
answered by 13.10.2016 / 08:17
source
1

What you are trying to do is called lookbehind , which would be to ask the language that when it finds what it does matchea (in this case the blank), check the previous part of the regex to validate it . Unfortunately Javascript does not support the lookbehind , so the closest thing to what you ask, would be to use some of these implementations or alternative solutions to the lookbehind that go around:

Javascript lookbehind

JS function that resolves the lookbehind

I hope you serve, greetings!

    
answered by 13.10.2016 в 04:20