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

const text = 'This is a #hash tag';
if(text.startsWith('#')){  
  console.log('The text starts with #');
} else {
  console.log('The text does not start with #');
}

输出结果为:The text starts with #

如果需要忽略大小写,可以使用 toLowerCase() 方法转换为小写后再判断:

const text = 'This is a #Hash tag';
if(text.toLowerCase().startsWith('#')){  
  console.log('The text starts with #');
} else {
  console.log('The text does not start with #');
}

输出结果仍为:The text starts with #

JavaScript 检查文本是否以 # 开头:代码示例

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

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