簡體   English   中英

正則表達式捕獲最后一個空格字符和字符串結尾之間的字符

[英]Regex to capture characters between the last white space character and the end of string

我正在嘗試確定最后一個白色空間字符和字符串結尾之間的字符。

Input:   "this and that"

Output: "that"

我試過下面的正則表達式,但它不起作用!

var regex = /[\s]$/

可以沒有正則表達式

var result = string.substring(string.lastIndexOf(" ")+1);

使用正則表達式

result = string.match(/\s[a-z]+$/i)[0].trim();

我建議你使用簡單的正則表達式模式

\S+$

Javascript測試代碼:

document.writeln("this and that".match(/\S+$/));

輸出:

that 

在這里測試一下

您可以刪除所有內容到最后一個空格。

s.replace(/.* /, '')

或者,匹配任何空白區域......

s.replace(/.*\s/, '')

您的示例僅匹配字符串末尾的一個空格字符。 使用

/\s\S+$/

匹配任何數字。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM