簡體   English   中英

如何通過忽略數字中的句點的標點符號分割字符串

[英]How to split string by punctuation that ignores the period in numbers

我在 javascript 中使用以下代碼將字符串拆分為短語。

var result = str.match( /[^\n\.!\?\;:]+[\n\.!\?\;:]+/g );
let elements = result.map(element => element.trim());
elements = elements.filter(function (el) {return el != null && el != "";});

它工作正常。 我的問題是,當字符串中的數以千計的數字標有一些人使用的點時,例如 1.500。 如何更改它以便僅在標點符號后跟空格時分隔短語。

您可以使用

/(?:[^\n.!?;:]|[\n.!?;:](?!\s))+[\n.!?;:]+/g

請參閱正則表達式演示 關鍵是你要么匹配除你選擇的標點符號之外的任何字符,要么匹配一個不帶空格的標點符號,一次或多次,然后是你選擇的一個或多個標點符號。

詳情

  • (?: - 非捕獲組的開始:
    • [^\n.?;::] - 除換行符以外的任何字符, . , ! , ? , ; :
  • | - 或者
    • [\n.?;:?](?!\s) - 換行符. , ! , ? , ; or :后面沒有空格
  • )+ - 一次或多次
  • [\n.?;::]+ - 一個或多個換行符, . , ! , ? , ; :字符。

請參閱 JavaScript 演示:

 var s = 'It works ok. My problem is when the string has numbers in the thousands marked with a dot that some people use like 1.500. How can alter this so that it only separates the phrases if the punctuation is followed by a space.'; var rx = /(?:[^\n.?;:.]|[\n?;:?.](?;\s))+[\n:;..;]+/g; console.log( s.match(rx) );

暫無
暫無

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

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