Meaning of part of the Javascript code

Asked

Viewed 332 times

2

I built a code to prevent my search tool from searching with the blank search field or with the default message that appears in value, I received a piece of code and when implementing an error that previously did not occur in layout, has a small part of the code that I do not have the knowledge of what it does and that before had not in the code and did not appear the error.

I have the following code:

<form name="produtosBuscaForm" method="get" action="http://busca.exemplo.com.br"  onsubmit="return ajaxsearchsubmit(this)">  

        <!--!! <input type="hidden" name="Categoria" value="0" id="categoria"> !!-->
        $&{<input type="hidden" name="view" value="(view)" />}       
                 <div class="busca">
         <div class="inputbusca">
            <input type="text" name="w" maxlength="35" value="Digite a busca" onfocus="if(this.value == 'Digite a busca') { this.value = ''; }" onblur="if(value=='') value ='Digite a busca'"id="sli_search_1" autocomplete="off" style="font-family:Verdana, Geneva, sans-serif;font-style:italic;font-weight:lighter;font-size:12px;color:#999999;outline:none;">
         </div>
                    <a href="javascript:return ajaxsearchsubmit(document.produtosBuscaForm);" class="ok"></a>
                 </div>                 
    </form>

My doubt is regarding the following part:

 <!--!! <input type="hidden" name="Categoria" value="0" id="categoria"> !!-->
        $&{<input type="hidden" name="view" value="(view)" />}   

The commented part OK, but what would be the command $&{ }? I wonder if this part might be causing me the error?

  • 1

    From what I understand (as described on this blog in English), $& returns the last string returned by a regular expression. In this context, I don’t know if this is what might be causing the error, it may be easier to determine if you mount an example in jsFiddle.

  • 1

    Tests if the input is empty or with default text, give some type of Alert and a false Return. It is no longer easy?

  • What error does appear? I have an answer almost ready to send to you.

  • No error appears exactly, the search works only that on the screen appears the code "$&{ }" I did not understand why they created a commented input followed by another input (which I see the error in question) and a third in the sequence (which is what actually works) I received this code and I am implementing in our site but I do not understand why it was assembled this way

  • 1

    Got it, this is wrong, just delete this $&command, because it should be in a <script> tag and not directly in html as there is.

  • I will remove and see if the features remain correct! I also thought it was wrong but was not going to remove without the certainty of being an error. Thank you

Show 1 more comment

1 answer

1

$& reference to function String.replace() javascript.

It simply means that replace should be done throughout the current text.

Here’s a simple example using $&:

if (typeof String.prototype.highlight !== 'function') {
    String.prototype.highlight = function (match, spanClass) {
        var pattern = new RegExp(match, "gi"),
            highlight = "<span>$&</span>";

        return this.replace(pattern, highlight);
    }
}

I set up a Jsfiddle with another simple example

Here is the jsfiddle code:

var value = "My number is 212-555-1234.";
var pattern = new RegExp( "(\\d+)", "g" );

var result = value.replace(
    pattern,
    "[$&]"
);

alert( result );

I hope it helps.

Browser other questions tagged

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