0

I want to take strings like:

  • Submit Changes
  • Create New
  • Update Record
  • Save Item

and convert them to:

  • Submitting Changes
  • Creating New
  • Updating Record
  • Saving Item

with a function like:

var ConvertToProgressivePresent = (function(){
    // cache this regex
    var rProgressivePresent = /\b(?:(Submi(t))|(Creat|Sav|Updat)e)\b/i;
    return function(phrase){
        return phrase.replace(rProgressivePresent, "$1$2$3ing");
    };
}());

This above regex works but doesn't seem like the best way to do it. I don't like the grouping here where 1-2 groups are always empty when there is a match.

Got any suggestions or improvements?

3
  • Regex is not the only tool. Computers do other things, too. Why do so many people jump to regex for any string manipulation? (This isn't aimed at you specifically; I've seen too many of these questions recently. Though in this case regex is probably not the best choice.) Commented Oct 28, 2010 at 17:24
  • Regex is the answer to all the worlds ills, didn't you know? Well, all except for HTML parsing apparently. Clearly HTML must be destroyed. Commented Oct 28, 2010 at 17:53
  • Because it is much more fun. Because it is challenging. Because it allows for concise (albeit, less manageable) code. :-) Commented Oct 28, 2010 at 18:03

3 Answers 3

2

If you have specific words to replace with then you could make a word bank. Have the words and their replacement stored in an object and then loop through it.

var ConvertToProgressivePresent = (function() {
    var replaceValues = {
        "Submit": "Submitting",
        "Create": "Creating",
        "Update": "Updating",
        "Save": "Saving"
    }
    return function(phrase) {
        for (var item in replaceValues) {
            phrase = phrase.replace(item, replaceValues[item]);
        }
        return phrase;
    };
}());

Here is a JSFiddle Example

Sign up to request clarification or add additional context in comments.

6 Comments

Nicely done. I'd probably use arrays instead of the hash though. Now how would you go about case-insensitivity; surely you wouldn't add more words to the bank?
Here is an update that is case-insensitive jsfiddle.net/subhaze/aVnhH/3
Cool. But what happens with the string "Submit Your Update"? It should only change the FIRST match.
Heres a test suite(ish): jsfiddle.net/3hJD5 and benchmark: jsperf.com/regex-vs-bank-regex
Made an update to the Fiddle you made, this version passes all tests. jsfiddle.net/3hJD5/1
|
1

I think you should probablly use CASE statments instead. Regex isn't the most efficient way of doing things...and that is probably best that it doesn't cuz you know the old saying.

Wow, I have this problem...I know, I'll use regex...ok, now you have two problems 90)

5 Comments

I second that. It will be much easier to add an extra case statement than to hack it in the regex.
Hm, can you demonstrate a good way to do this with a switch/case? if-else-if seems like it would be better than case for this.
I'm not really a java guy but couldn't you do it like this one? stackoverflow.com/questions/3290550/javascript-switch-case
haha. No you are not. You are probably not much of a Javascript guy eithe. :-p They are VERY different languages.
@David Murdoch HA!! nor am I much of a "interupt me while I'm posting a comment on SO" guy either 80))
0

First off, it doesn't appear to me that your regex does quite what you want anyway in that I don't see a second "t" added when changing submit to submitting.

However, I don't think I would use regex for this task at all anyway. If you are just trying to replace one word with another, and the word always comes at the beginning of the string, I might do something like:

function ReplaceIfBeginsWith(wholeString, checkFor, replaceWith)
{
    if (wholeString.indexOf(checkFor + ' ') == 0)
    {
        return replaceWith + ' ' + wholeString.substr(checkFor.length + 1);
    }
    if (wholeString == checkFor)
    {
        return replaceWith;
    }
    return wholeString;
}

Then, you can call the function with each of the words you would want to replace. If you want case-insensitivity, just check against lowercase versions of all the strings.

1 Comment

The second "t" does get added, BTW.

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.