检查字符串是否以 [英] Check if string ends with

查看:101
本文介绍了检查字符串是否以的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查字符串是否以

- v{number}

所以例如

hello world           false
hello world - v2      true
hello world - v       false
hello world - v88     true

不完全确定如何进行此RegEx。

Not entirely sure how to go about doing this RegEx.

var str = 'hello world - v1';
var patt = new RegExp("/^ - v\d*$/");
var res = patt.test(str);
console.log(res);

我如何修复上述RegEx?

How could I fix the above RegEx?

推荐答案

只需使用它而不检查开头的任何其他内容

Just use this without checking anything else in the beginning

- v\d+$

这样你就可以确保它以结尾 - v 后跟至少一位数。请参阅 https://regex101.com/r/pB6vP5/1

This way you make sure it ends with - v followed by at least one digit. See it live in https://regex101.com/r/pB6vP5/1

然后,你的表达式必须是:

Then, your expression needs to be:

var patt = new RegExp("- v\\d+$");

as anubhava在另一个答案中声明


  • 否需要 /

  • 需要双重转义 \d

  • No need for /
  • Requires double escaping for \d.

var str = 'hello world - v15';
var patt = new RegExp("- v\\d+$");
var res = patt.test(str);
console.log(res);

这篇关于检查字符串是否以的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆