endsWith in JavaScript

前端 未结 30 1625
-上瘾入骨i
-上瘾入骨i 2020-11-22 05:41

How can I check if a string ends with a particular character in JavaScript?

Example: I have a string

var str = \"mystring#\";

I wa

30条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 06:28

    So many things for such a small problem, just use this Regular Expression

    var str = "mystring#";
    var regex = /^.*#$/
    
    if (regex.test(str)){
      //if it has a trailing '#'
    }

提交回复
热议问题