18

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.

3
  • Actually, "*Foo*".replace(/\x2a/g, '%') worked for me. What was the problem with that one? Or which browser did it fail in? Commented Oct 2, 2009 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). Commented Oct 2, 2009 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. Commented Oct 2, 2009 at 15:12

6 Answers 6

42

Try:

queryString = inputText.replace(/\*/g, '%');
Sign up to request clarification or add additional context in comments.

1 Comment

So simple, yet so hard. Awesome :)
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("%");

3 Comments

I've never seen this approach, but I'll certainly do some of my own testing and try it out. Thanks!
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.
Here's an explanation I found: stackoverflow.com/questions/441018/…
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

Comments

1

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

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

Comments

0

You can use:

queryString = inputText.replace(/[*]/g,"");

Comments

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.