What Regular Expression to Trace a URL

6

Well, I'm asking because I do not know, but I'm trying ...

Doubt

http://www.meusite.com.br/busca.html?nome=louvor+a+Deus

Where: ?nome=louvor+a+Deus

  

You should remove or better replace with% with spaces

? = +

I have the replace(); table for consultation at hand, but I'm still a bit lay about it.

    
asked by anonymous 09.06.2016 / 02:12

1 answer

10

In this case we are not, checking a complete URL, just what was requested, if it were a URL would be much more complex, even to ask the question ...

Try so boss:

var str = 'http://www.meusite.com.br/busca.html?nome=louvor+a+Deus'
str = str.replace(/\?|=|\+/g,' ')
alert(str)

If you like it, it fits ...

Following the suggestion of the master @Randrade, I will give an explanation, in the regex:

The slash "/" at the beginning and at the end "/" delimit the expression.

The backslash "\" serve to escape special characters (?, +), since they all have a function in the regex, but as we want to use them literally you have to escape with a "\" backslash .

The vertical bars "|", represent, "or" ...

And the "g" at the end is a flag, it matches the same occurrence several times, as in the case of "+", it is only declared once.

Here teaches some women, Aurélio.net

    
09.06.2016 / 04:02