Skip to content

Commit

Permalink
[demo/survey-replace] JS doesn't support $0, it has $& instead!
Browse files Browse the repository at this point in the history
Our Eggex API has _group(0) already, so I think we should have $0.
  • Loading branch information
Andy C committed Jan 22, 2024
1 parent 521a799 commit de37673
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions demo/survey-replace.sh
Expand Up @@ -59,6 +59,11 @@ regex() {
python3 -c 'import re; p = re.compile(""); print(p.sub("++", "oils-for-unix"))'
echo

# supports equivalent of $0 and $1 ?
python3 -c 'import re; p = re.compile("[i](.)"); print(p.sub("[\g<0>]", "oils-for-unix"))'
python3 -c 'import re; p = re.compile("[i](.)"); print(p.sub("[\g<1>]", "oils-for-unix"))'
echo

echo 'REGEX JS'
echo

Expand All @@ -74,6 +79,12 @@ regex() {

# Hm this is inconsistent -- empty string gets replaced everywhere
nodejs -e 'console.log("oils-for-unix".replace(new RegExp("", "g"), "++"))'

# Hm JavaScript does not support $0 for the whole match -- it has $& instead
# https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
nodejs -e 'console.log("oils-for-unix".replace(new RegExp("[i](.)", "g"), "[$0]"))'
nodejs -e 'console.log("oils-for-unix".replace(new RegExp("[i](.)", "g"), "[$&]"))'
nodejs -e 'console.log("oils-for-unix".replace(new RegExp("[i](.)", "g"), "[$1]"))'
}

"$@"

0 comments on commit de37673

Please sign in to comment.