确定字符串开头的给定类型是否多于另一个

Determine if a string starts with more letters of a given kind than the other

本文关键字:是否 另一个 类型 字符串 开头      更新时间:2023-09-26
var s1 = "-Hello";
var s2 = "--Whats up?";
var s3 = "How you doing?";

我如何比较字符串,并提出问题;"这个字符串是否以比另一个更多的'-'开头?

function count( str ){
   var match = str.match(/(-+)/);
   return match ? match[0].length : 0;
}
console.log( count("--qwerty") );

或者如果你想传入主角

function count( char, str ){
   var newRE = new RegExp( "(" + char + "+)","" );
   var match = str.match( newRE );
   return match ? match[0].length : 0;
}
console.log( count("-", "--qwerty") );
相关文章: