在第 n 次出现空格后删除所有文本
delete all text after the nth occurance of a whitespace
我使用此代码段在文本中创建换行符。我怎样才能完全删除第n个空格后面的文字?
var str:String = ("This is just a test string").replace(/(( [^ ]+){2}) /, "\n");
问候
这可以使用正则表达式 (([^ ]* ){2}).*
并替换模式 </code>:</p>
<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre><code>function removeAfterNthSpace() {
var nth = parseInt($("#num").val());
var regEx = new RegExp("(([^ ]* ){" + nth + "}).*", "g")
var str = ("This is just a test string").replace(regEx, "");
console.log(str);
}
$('#num').change(removeAfterNthSpace);
removeAfterNthSpace();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="number" id="num" value="2" />
我使用此代码段在文本中创建换行符。我怎样才能完全删除第n个空格后面的文字?
var str:String = ("This is just a test string").replace(/(( [^ ]+){2}) /, "\n");
问候
这可以使用正则表达式 (([^ ]* ){2}).*
并替换模式 </code>:</p>
<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre><code>function removeAfterNthSpace() {
var nth = parseInt($("#num").val());
var regEx = new RegExp("(([^ ]* ){" + nth + "}).*", "g")
var str = ("This is just a test string").replace(regEx, "");
console.log(str);
}
$('#num').change(removeAfterNthSpace);
removeAfterNthSpace();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="number" id="num" value="2" />