简体   繁体   中英

ReplaceAll Not Working [JS]

if(command === 'bigletters')
  {
  String.prototype.replaceAll = function(str1, str2, ignore) 
{
    return this.replace(new RegExp(str1.replace(/([\/\,\!\\\^\$\{\}\[\]\(\)\.\*\+\?\|\<\>\-\&])/g,"\\$&"),(ignore?"gi":"g")),(typeof(str2)=="string")?str2.replace(/\$/g,"$$$$"):str2);
let str = args.join(" ")
      let olda = "a"
      let oldb = "b"
      let oldc = "c"
  let oldd = "d"
  let olde = "e"
  let oldf = "f"
  let oldg = "g"
  let oldh = "h"
  let oldi = "i"
  let oldj = "j"
  let oldk = "k"
  let oldl = "l"
  let oldm = "m"
  let oldn = "n"
  let oldo = "o"
  let oldp = "p"
  let oldq = "q"
  let oldr = "r"
  let olds = "s"
  let oldt = "t"
  let oldu = "u"
  let oldv = "v"
  let oldw = "w"
  let oldx = "x"
  let oldy = "y"
  let oldz = "z"
  str.replaceAll(olda, ":regional_indicator_a:");
  str.replaceAll(oldb, ":regional_indicator_b:");
  str.replaceAll(oldc, ":regional_indicator_c:");
  str.replaceAll(oldd, ":regional_indicator_d:");
  str.replaceAll(olde, ":regional_indicator_e:");
  str.replaceAll(oldf, ":regional_indicator_f:");
  str.replaceAll(oldg, ":regional_indicator_g:");
  str.replaceAll(oldh, ":regional_indicator_h:");
  str.replaceAll(regi, ":regional_indicator_i:");
  str.replaceAll(oldj, ":regional_indicator_j:");
  str.replaceAll(oldk, ":regional_indicator_k:");
  str.replaceAll(oldl, ":regional_indicator_l:");
  str.replaceAll(oldm, ":regional_indicator_m:");
  str.replaceAll(oldn, ":regional_indicator_n:");
  str.replaceAll(oldo, ":regional_indicator_o:");
  str.replaceAll(oldp, ":regional_indicator_p:");
  str.replaceAll(oldq, ":regional_indicator_q:");
  str.replaceAll(oldr, ":regional_indicator_r:");
  str.replaceAll(olds, ":regional_indicator_s:");
  str.replaceAll(oldt, ":regional_indicator_t:");
  str.replaceAll(oldu, ":regional_indicator_u:");
  str.replaceAll(oldv, ":regional_indicator_v:");
  str.replaceAll(oldw, ":regional_indicator_w:");
  str.replaceAll(oldx, ":regional_indicator_x:");
  str.replaceAll(oldy, ":regional_indicator_y:");
  str.replaceAll(oldz, ":regional_indicator_z:");
  msg.channel.sendMessage(str + " '''" + str + "'''")
}`

That is the code I am trying to exe for a discord bot, I have it so when someone says -bigletters and then the message they want, exe -bigletter I like pie I want it to replace every letter with an emoji to make it 'big letters' as it states, but it doesn't work, it outputs with 'abcd' or 'abcd' when I do -bigletters abcd or abcd

Could someone help me out here please?

Just… don't do that:

if (command === 'bigletters') {
  const str = args.join(' ');
  const replaced = str.replace(/[a-z]/g, ':regional_indicator_$&:');
  msg.channel.sendMessage(str + " '''" + replaced + "'''")
}

String.prototype.replace takes a regular expression. If the regular expression has the g flag, it will replace all occurrences in a string. /[az]/ matches lowercase “a” to “z”. $& in the replacement is the matched text.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM