正規表現と文字列のマッチングが必要です。私の文字列(var strで表されます)の最後にダッシュと整数があるかどうかをチェックするにはどうすればいいですか?次の例を考えてみましょう:文字列の末尾にダッシュ整数があるかどうかを調べる
Example 1:
var str = "test101-5"
evaluate the str and check if it end with a dash and an integer { returns true }
Example 2:
var str = "ABC-DEF-GHI-4"
evaluate the str and check if it end with a dash and an integer { returns true }
Example 3:
var str = "test101"
evaluate the str and check if it end with a dash and an integer { returns false }
卓越した!整数の値を取得する方法はありますか? – BobbyJones
はい、一度マッチすると、最後の文字を取得して、 '+': '+ str.substr(-1) 'で数値にすることができます。より一般的には、 'String#match'メソッドでマッチを取得することができます:' str.match(/ - (\ d)$ /)[1] '。しかし、 'match'は一致がないときに' null'を返すことに注意してください。 – trincot
@BobbyJones RegExpキャプチャグループを使用することができます – Cheloide