loading...

Discussion on: Snake case to camel case and back using regular expressions and Python

Collapse
bernhardwebstudio profile image
Bernhard Webstudio

Coming here from Google, I will answer my question using your code, thanks! That's how to replace all words etc. from snake_case to camelCase in all js files in a directory:


import glob
import re
from pathlib import Path

rootdir = "C:\\Users\\Your User\\Your Path"

REG = r"(.*?)_([a-zA-Z])"


def camel(match):
    return match.group(1) + match.group(2).upper()


for filename in glob.glob(rootdir + "\\**\\*.js"):
    print(filename)
    text = Path(filename).read_text()
    text = re.sub(REG, camel, text, 0)
    file = open(filename, 'w')
    file.write(text)
    file.close()