17

Using JQuery, I'm extracting the value from what is essentially a query box for some data in a MySQL database. Knowing that most users will use an '*' (asterisk) as a wildcard value and that MySQL uses the '%' character, I want to convert any asterisks to '%'.

Normally, this would just be as simple as using

queryString = inputText.replace('*', '%');

but I've had little success with that. Javascript's replace() function only replaces the first occurrence, so it's not replacing the last asterisk in something like *Foo*

The second option I tried was using a regular expression, which also didn't work:

queryString = inputText.replace(/\x2a/g, '%');

How can I replace the '*' (asterisks) with a '%' (percent sign)? I'd imagine there's a really simple regular expression, or something I'm overlooking.

S Pangborn
  • 12,593
  • 6
  • 24
  • 24
  • Actually, `"*Foo*".replace(/\x2a/g, '%')` worked for me. What was the problem with that one? Or which browser did it fail in? – Roatin Marth Oct 02 '09 at 14:59
  • It's highly possible I could have botched something on that one. I just tested it again and it's working - weird. I'm testing in Firefox 3.5 nightlies (Shiretoko) and Epiphany (webkit). – S Pangborn Oct 02 '09 at 15:08
  • Also, it could have easily been something that was fixed in the newest nightly. I should have kept the old build around to test in. – S Pangborn Oct 02 '09 at 15:12

6 Answers6

39

Try:

queryString = inputText.replace(/\*/g, '%');
Roatin Marth
  • 23,589
  • 3
  • 51
  • 55
5

Splitting a string into an array and then joining it back into a string is faster than regular expression replacements:

queryString = inputText.split("*").join("%");
Ates Goral
  • 137,716
  • 26
  • 137
  • 190
  • I've never seen this approach, but I'll certainly do some of my own testing and try it out. Thanks! – S Pangborn Oct 02 '09 at 15:04
  • Interesting. Would need some benchmark, both on CPU and memory... Unless you do replacement on a large text, I doubt we see much speed gain. – PhiLho Oct 02 '09 at 15:08
  • 2
    Here's an explanation I found: http://stackoverflow.com/questions/441018/replacing-spaces-with-underscores-in-javascript/441035#441035 – Roatin Marth Oct 02 '09 at 15:14
1

If you want to try passwords with a nice touch for remembering

str.replace(str.substr(1,str.length-3), str.substr(1,str.length-3).replace(/./g,"*"))

The output will be

1*********xY
TorNadO
  • 151
  • 1
  • 6
1

Actually, you don't need a regular expression. You can simply use replaceAll:

queryString = inputText.replaceAll("*", "%");
0

You can use:

queryString = inputText.replace(/[*]/g,"");
Pang
  • 9,564
  • 146
  • 81
  • 122
Milos Cuculovic
  • 19,631
  • 51
  • 159
  • 265
0

I use this function to hide some digits of a phone numbers

function replacePhoneNumbers(number){
    var cuantos = number.length;
    var tele = [];
    for (var i = 0; i < cuantos; i++) {
        if(i < 7){
            tele[i] = number[i].replace(/^\d+$/, "*");
        }
        else{
            tele[i] = number[i];
        }
    }
    
    var full_phone = "";
    for (var i = 0; i < cuantos; i++) {
        full_phone += tele[i];
    }
    return full_phone;
    //return number;
}

var number = replacePhoneNumbers("7533930048");
document.getElementById("demo").innerHTML = number;
<p id="demo"></p>