LoginSignup
3
1

More than 3 years have passed since last update.

配列内の文字列を置換する

Last updated at Posted at 2020-11-24

配列内の文字列を置換したい

userNameをたけしくんにする覚えがきです

index.js
const array = ["userNameくんかっこいいよね","userNameくんどこに住んでるの" ,"userNameくん乳首透けてるよ"]
index.js
  //配列を展開
  array.forEach((text) => {
    //指定した文字列が入っているか判断する(userName)
      const stringInUserName = text.includes("userName");
      if (stringInUserName) {
  //tureならuserNameをたけしくんにする
        text = text.replace("userName", "たけし");
      }
      return text;
    });
index.js
  たけしんくんかっこいいよね たけしくんどこにすんでるの たけしくん乳首透けてるよ

以上

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1