4

I've a regex that will only match one character of the strings. I want to test the lentgh of its containing string and if it was greater than 4 then make the replacement. For example, the regex is /\d/. I want to use the functional form of replace to match 12345 but not 1234.

Something like:

text.replace(regex, function(match) {
       if (STRING.length > 4)
            return replacement
       else
            return match;
  });

Note: /\d/ is just an example. I didn't mention the real regex to focus on my real question, illustrated above.

Iryn
  • 255
  • 2
  • 5
  • 13
  • 1
    for all those like me who didn't know you could pass a function to the replace function: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/replace – Sebas Apr 28 '13 at 13:48

3 Answers3

5

Or if you want to do it that way:

function replaceWithMinLength (str, minLength) {
   str.replace(/\w+/, function(match) {
      if (match.length > minLength) {
        return match;
      } else {
        return str;
      }
   });
}
Chris
  • 6,331
  • 1
  • 21
  • 25
  • [**This**](http://jsfiddle.net/627rF/1/) is the original, and [**this**](http://jsfiddle.net/627rF/2/) is trying to implementing your code. – Iryn Apr 28 '13 at 17:14
  • Ok, I see the problem but I dont know what your actual goal is. Can you give the the result or wich characters should be red? – Chris Apr 28 '13 at 18:13
  • I probabely should have used a better example. Please see [this fiddle](http://jsfiddle.net/627rF/4/). The first line should not have any red character, as the number have less than five digits. – Iryn Apr 28 '13 at 19:02
  • Also, if more information is needed, see [my previous question](http://stackoverflow.com/questions/16241625/why-this-regex-does-not-work-with-eastern-arabic-numerals). – Iryn Apr 28 '13 at 19:05
  • Ah, so it would make sense to check the length before applying the regex replace right? http://jsfiddle.net/XpjF2/ – Chris Apr 28 '13 at 19:42
  • Thanks, but there's still a problem :) [http://jsfiddle.net/XpjF2/2/](http://jsfiddle.net/XpjF2/2/) – Iryn Apr 28 '13 at 19:54
  • Better illustrated here [http://jsfiddle.net/XpjF2/3/](http://jsfiddle.net/XpjF2/3/). – Iryn Apr 28 '13 at 19:59
  • 1
    Ok, now im sure that this must be possible with regex. But with javascript: http://jsfiddle.net/7X7em/1/ – Chris Apr 28 '13 at 20:50
  • Aslo, can you please tell me what do you think about its performance hit? Is this something to worry about? – Iryn Apr 28 '13 at 21:12
  • 1
    Well I do not advise to use this method, but performance will not suffer that much I guess. Think of 3D canvas calculations browser do these days ;) – Chris Apr 28 '13 at 21:53
4

You're putting the horse before the cart. You would be better off:

if(string.length > 4) {
  string.replace('needle','replacement');
}
oomlaut
  • 763
  • 4
  • 12
0

So by “containing string”, you mean like the same sequence of digits? Match them all at once:

text.replace(/\d{5,}/g, function(string) {
    return string.replace(/\d/g, function(match) {
        return replacement;
    });
});

For example. The \d{5,} can easily be adapted to any type of string-thing.

Ry-
  • 218,210
  • 55
  • 464
  • 476
  • @Iryn: In what way? Can you show your code, or a [short example](http://codepen.io/pen)? – Ry- Apr 28 '13 at 15:01
  • Please see [this question](http://stackoverflow.com/questions/16241625/why-this-regex-does-not-work-with-eastern-arabic-numerals). – Iryn Apr 28 '13 at 15:11
  • How to it for number. Example : How I would set all number below 40,000 to 0 in a text files *(they are delimited by commas semicolons full dots and spaces )* – user2284570 Jul 16 '15 at 13:23