我想将一个段落分成jquery中的句子。可以说我有一段
This is a wordpress plugin. its older version was 2.3.4 and new version is 2.4. But the version 2.4 had a lot of bungs. Can we solve it?
我想把它分解成
This is a wordpress plugin.
its older version was 2.3.4 and new version is 2.4.
But the version 2.4 had a lot of bungs.
Can we solve it?
有什么解决方案吗?我试图使用此功能,但当数字到来时它也将句子分开。
var result = str.match( /[^\.!\?]+[\.!\?]+/g );
谢谢
您可以使用类似的方法/((\.|\?|\!)\s)|(\?|\!)|(\.$)/g
来获取元素。这是每个捕获组的伪分解:
((\.|\?|\!)\s)
:任意.
,?
或!
后跟空白。(\?|\!)
:任何独立的?
或!
。(\.$)
:任何.
后跟end-of-line
。(根据字符串,这一点可能是不必要的)这是使您步入正轨的粗略代码:
console.clear();
var str = 'This is a wordpress plugin. its older version was 2.3.4 and new version is 2.4. But the version 2.4 had a lot of bungs. Can we solve it?';
console.log('"' + str + '"');
console.log('Becomes:');
console.log('"' + str.replace(/((\.|\?|\!)\s)|(\?|\!)|(\.$)/g, ".\n") + '"');
为了解决不同的符号,“真实交易”将必须适当地替换几轮才能解决:
console.clear();
var str = 'This is a wordpress plugin. its older version was 2.3.4 and new version is 2.4. But the version 2.4 had a lot of bungs. Can we solve it?';
str = str
//"all"
//.replace(/((\.|\?|\!)\s)|(\?|\!)|(\.$)/g,".\n")
//"."
.replace(/((\.)\s)|(\.$)/g, ".\n")
//"?"
.replace(/((\?)\s)|(\?)/g, "?\n")
//"!"
.replace(/((\!)\s)|(\!)/g, "!\n")
console.log(str)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句