search()是TypeScript中的内置函数,用于搜索正则表达式与此String对象之间的匹配项。句法:
string.search(regexp);
参数:该方法接受上述和以下描述的单个参数:
- regexp:此参数是RegExp对象。
返回值:此方法返回字符串内的正则表达式的索引。否则,它返回-1。
下面的示例说明TypeScriptJS中的String search()方法:
范例1:
的JavaScript
<script>
// Original strings
var str = "Geeksforgeeks - Best Platform";
var re = /Best/gi;
// use of String search() Method
if (str.search(re) == -1 ) {
console.log("Not Found" );
} else {
console.log("Found" );
}
</script>
输出:
Found
范例2:
的JavaScript
<script>
// Original strings
var str = "Geeksforgeeks - Best Platform";
var re = /geeek/gi;
// use of String search() Method
if (str.search(re) == -1 ) {
console.log("Not Found" );
} else {
console.log("Found" );
}
</script>
输出:
Not Found
相关用法
- Typescript String charAt()用法及代码示例
- Typescript String charCodeAt()用法及代码示例
- Typescript String concat()用法及代码示例
- Typescript String indexOf()用法及代码示例
- Typescript String lastIndexOf()用法及代码示例
- Typescript String localeCompare()用法及代码示例
- Typescript String replace()用法及代码示例
- Typescript String slice()用法及代码示例
- Typescript String substring()用法及代码示例
- Typescript String valueOf()用法及代码示例
- Typescript String toString()用法及代码示例
- Typescript String toUpperCase()用法及代码示例
- Typescript String toLowerCase()用法及代码示例
- Typescript String toLocaleUpperCase()用法及代码示例
- Typescript String toLocaleLowerCase()用法及代码示例
- Typescript String substr()用法及代码示例
- Typescript String split()用法及代码示例
注:本文由纯净天空筛选整理自SHUBHAMSINGH10大神的英文原创作品 TypeScript | String search() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。