1

Trying to find a regex pattern that will match a repeating string of words separated by white space, but only if it's at the beginning of a larger string.

"Copy of Copy of Copy of This is my String"

I want to get rid of all the "Copy of " strings, so the result is:

"This is my String"

The "Copy of " can occur an unknown number of times. Want to get rid of all of them, but only if they start at the beginning of the string.

I tried: \G(\Copy of )( (?=\1 ))? This was from an example I saw but couldn't figure out how to modify it to work as I want.

ciso
  • 2,887
  • 6
  • 33
  • 58
  • 1
    Did you try googling for javascript and regex and reading to understand, rather than reading for the exact example? – user3791372 Jun 03 '16 at 00:36
  • ... did you try replace? – user3791372 Jun 03 '16 at 00:38
  • Sounds like homework to me if you have to use regex. Read your notes and learn to search for the info - it'll be the most important skill you'll learn. – user3791372 Jun 03 '16 at 00:41
  • Also, you need to clarify what "repeating strings" means. Is an individual character a string? In "A A A x" would be a repetitive "A " string. But in "AAA x", would "A" also be a repetitive string? – le_m Jun 03 '16 at 00:46
  • So, if you have "AAAAx", then the "AAAA" part could e.g. be a repetition of string "AA" (2 times)? Or does a string have to end on a non-word character? Whitespace? – le_m Jun 03 '16 at 01:25
  • Possible duplicate of [How do you access the matched groups in a JavaScript regular expression?](http://stackoverflow.com/questions/432493/how-do-you-access-the-matched-groups-in-a-javascript-regular-expression) –  Jun 03 '16 at 01:51
  • 1
    The question appears to be about removing a repeated group rather than accessing a capturing group per se. – 1983 Jun 03 '16 at 02:14
  • You first mention removing a 'repeating' string, but later state the string can occur 'an unknown number of times'. Does that include a single occurrence as in `Copy of This is my String`? – le_m Jun 03 '16 at 02:34
  • 1
    Looks like Windows automatically-generated filenames to me. – 1983 Jun 03 '16 at 02:59

2 Answers2

2

To match repetitive strings (without knowing the exact string), you need to use capturing groups and backreferences.

Depending on the definition of 'string', there are multiple solutions, e.g.:

  • Any character sequence: ^(.+?)\1+

    • Matches e. g. "AAA" of "AAA B", "AA" of "AA AA B", whitespace
  • Character sequence ending on whitespace: ^(.+?\s)\1+

    • Matches e. g. "AA AA " of "AA AA B", whitespace
  • Character sequence ending on word boundary: ^(.+?\b)\1+

    • Doesn't match whitespace. This is probably what you want.

If you want to match the remaining, non-repetitive part of the string, simply add a second capturing group (.*) to the above regex:

// Get the repeated word:
console.log('Copy of Copy of Copy of This is my String'.match(/^(.+?\b)\1+(.*)/)[1]);
            
// Get the remaining string:
console.log('Copy of Copy of Copy of This is my String'.match(/^(.+?\b)\1+(.*)/)[2]);

Explanation: ^ matches the start of our string, the capturing group (.+?\b) matches any character one or more times followed by a word boundary and allows us to reference to its match later via \1. Since we are looking for one or more repetitions, we try to match against \1+.

I recommend https://regex101.com/#javascript or similar tools to better understand and explore regex.

le_m
  • 19,302
  • 9
  • 64
  • 74
1

Please see the RegExp reference on MDN. This explains how RegExp patterns in JavaScript work.

  • For 'beginning of string', use ^.
  • For 'zero or more times', use *.
  • For 'one or more times', use +.

Either of the last two will work in this case.

Putting it all together:

var re = /^(Copy of )*/;
console.log('Copy of Copy of Copy of This is my String'.replace(re, ''));
console.log('Copy of This is my String'.replace(re, ''));
console.log('This is my String'.replace(re, ''))
1983
  • 5,882
  • 2
  • 27
  • 39
  • 1
    No worries, but please have a good read through that documentation before asking more questions :-). Even if you don't get it right away, it should help point you in the right direction for further research. – 1983 Jun 03 '16 at 00:53