可以使用字符串的 startsWith() 方法来判断一段文本是否以 # 开头,具体代码如下:

const text = 'Hello, world!';
if (text.startsWith('#')) {
  console.log('The text starts with #');
} else {
  console.log('The text does not start with #');
}

如果文本以 # 开头,就会输出'The text starts with #';如果不是以 # 开头,就会输出'The text does not start with #'。

JavaScript 判断文本是否以 # 开头 - 代码示例

原文地址: https://www.cveoy.top/t/topic/lRkV 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录