Adapt JS regex to fix a hanging issue