0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【JavaScript】substrメソッドの使い方

Last updated at Posted at 2021-03-02

substr()

String.prototype.substr()

文字列の一部を、指定した位置から後方向指定した文字数だけ返す

//公式
str.substr(start[, length])

//サイト記事
var str = 文字列
 
str.substr( 開始位置, 文字数 )

これが元になって調査

watch: {
    // 入力された文字列を監視する
    inputText: function() {
        var pos = this.inputText.indexOf(this.forbiddenText); // indexOf:指定された値が最初に現れたインデックスを返す
        if (pos >= 0) {
            alert(this.forbiddenText + "は、入力できません。");
            // 入力文字列から禁止文字を削除する
            this.inputText = this.inputText.substr(0, pos); //str.substr( 開始位置, 文字数(indexOfででたインデックス) )
        }
    }
}

参考

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?