Search code examples
regexidewebstorm

Find any string that starts with a specified search word and is followed by another capital word


We have some bad code that has been written where there is a redundant word in object property names and we're trying to find these culprits throughout the entire application using search and replace.

To give you an example, imagine a person object having its properties also contain the word person, i.e.

var person = new Person();
person.personName = 'Mark';
person.personAge = 20;

In this instance, we know the word person but don't know all of the properties that have been set on that object throughout an application.

Using Regex, are we able to find anything matching person*, where the wildcard is always a capital letter, i.e. camelCase? We want to replace personName with just name.

I thought about searching for .person but it's given me lots of unwanted results.


Solution

  • This regular expression works for me in sublime (not sure which IDE you're using) when I search the project:

    \.person[A-Z]

    explained:

    \. is to match a period (you have to escape it in a regular expression because otherwise it means match any single character)

    person is to match just person

    [A-Z] is to match any capital character