我正在寻求帮助,允许我的用户缩小某些文字输出的格式,以符合他们的偏好。
示例:
User list Summary:
Title:
- Big Pink
Student:
- Philip J Fry
Grade:
- A
Issues:
- This issue
- That issue
Improvements:
- This thing
- That thing
- The other thing
我希望能够将单个答案缩短到一行,但只留下多行答案。这样就变成了:
User list Summary:
Title: Big Pink
Student: Philip J Fry
Grade: A
Issues:
- This issue
- That issue
Improvements:
- This thing
- That thing
- The other thing
我到目前为止:https://jsfiddle.net/hematogones/0d4g0akh/
感谢您的帮助!
答案 0 :(得分:0)
你可以使用这个,使用负向前瞻来确保你只在冒号之后折叠一个带连字符的行,当没有其他人跟随它时:
text.replace(/:\r?\n-\h*(.*)$(?!\r?\n-)/gm, ": $1" ).replace(/^\s*\r?\n/gm,'');
const text = $("#outPut").val();
$(".switch").on("click", function(){
var text_new = "";
if (this.id == "switchto"){
text_new = text.replace(/:\r?\n-\h*(.*)$(?!\r?\n-)/gm, ": $1" ).replace(/^\s*\r?\n/gm,'');
$("#outPut").val(text_new);
}
if (this.id == "switchback"){
$("#outPut").val(text);
}
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id="outPut" style="width:60%; min-height:180px">
User list Summary:
Title:
- Big Pink
Student:
- Philip J Fry
Grade:
- A
Issues:
- This issue
- That issue
Improvements:
- This thing
- That thing
- The other thing
+ Final score:
- 78%
</textarea>
<br>
<input type="button" class="switch" id="switchto" value="Switch synoptic">
<input type="button" class="switch" id="switchback" value="Switch back">
&#13;