我有以下字符串 原始字符串
"1 st-May-2014"
"2 nd-May-2014"
"3 rd-May-2014"
"14 th-May-2014"
输出
"1st-May-2014"
"2nd-May-2014"
"3rd-May-2014"
"14th-May-2014"
现在我想在JavaScript中使用RegEx从上面的字符串中移除 st,nd,rd和th 之前的空格。
答案 0 :(得分:4)
这是我的建议:
result = subject.replace(/\s+(?=(?:st|nd|rd|th)\b)/g, "");
<强>解释强>
\s+ # Match whitespace
(?= # if the following text can be matched after it:
(?:st|nd|rd|th) # one of the four "words"
\b # that end there (so as not to match "4 stones")
) # End of lookahead assertion
答案 1 :(得分:2)
我想,你需要这个:
result = subject.replace(/^(\d+)\s+(.*)$/gm,"$1$2");