首页 > 解决方案 > Javascript:在最后两个特定字符之间提取字符串

问题描述

我有字符串,其中每个单词后跟一个点,例如 示例 1:示例 2 I.love.watching.movies. hello.I.am.a.football.fan. 我想提取最后一个单词,然后是它的点,例如示例 2我想提取字符串fan. 如何实现这个?

标签: javascript

解决方案


您可以使用String#match.

let str = 'hello.I.am.a.football.fan.';
let res = str.match(/.*?\./g).pop();
console.log(res);

正则表达式的解释:

  • .*?:匹配任何字符,除了行终止符,任意次数,懒惰。
  • \.: 匹配字符, ..

推荐阅读