用于查找和格式化文本部分的 Jquery 正则表达式

发布于 2024-11-27 10:13:02 字数 548 浏览 4 评论 0原文

我使用 Jquery 有许多动态生成的反馈行来测验来自 xml 的问题。示例:

您的回答(是。)您的患者处于中度至高风险。
您的回答(否。)您的患者处于低至高风险。
您回答(每周剂量。)您的患者处于中等风险。
等等。

我如何查找反馈文本的不同部分并对其进行格式化?我需要将部分文本的格式设置为斜体引号。还要将末尾的部分文本格式化为粗体,从单词“at”开始直到句点标记?

我想用正则表达式是可能的,但我还没有使用它们。 到目前为止,我有以下内容:

var feedbackString = "<p id='feedbackTxt'>" + currentQuizNode.find("Feedback").text() + "</p>";
$("#quizFeedback").append(feedbackString);

预先感谢您, 阿提拉

I used Jquery to have many dynamically generated feedback lines to quiz questions coming from an xml. Sample:

You answered (Yes.) Your patient is at moderate to high risk.
You answered (No.) Your patient is at low to high risk.
You answered (Weekly Dose.) Your patient is at moderate risk.
etc.

How can I find and format different parts of the feedback text? I need to format part of the text with the quotation marks in italic. Also format part of text at the end in bold starting with the word "at" till the period mark?

I guess it would be possible with regular expressions but I haven't used them.
so far I have the following:

var feedbackString = "<p id='feedbackTxt'>" + currentQuizNode.find("Feedback").text() + "</p>";
$("#quizFeedback").append(feedbackString);

Thank you in advance,
Attila

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

假情假意假温柔 2024-12-04 10:13:02
currentQuizNode.find("Feedback").text().replace(/(.*\()(.*)(\).*patient is\s)(.*)/,"$1<span class=\"answer\">$2</span>$3<span class=\"risk\">$4</span>")

非常丑陋,但它有效:P

currentQuizNode.find("Feedback").text().replace(/(.*\()(.*)(\).*patient is\s)(.*)/,"$1<span class=\"answer\">$2</span>$3<span class=\"risk\">$4</span>")

very ugly but it works :P

软糖 2024-12-04 10:13:02
var feedbackString = "<p id='feedbackTxt'>" + 
  currentQuizNode.find("Feedback").text().
    replace(/"(.*)"/, "<span style='font-style: italic;'>$1</span>").
    replace(/(\bat\b.*?\.)/, "<span style='font-weight: bold;'>$1</span>") + 
    "</p>";
$("#quizFeedback").append(feedbackString);

如果您想说的是括号而不是引号,则将:更改

replace(/"(.*)"/, "<span style='font-style: italic;'>$1</span>").

为:

replace(/(\(.*?\))/, "<span style='font-style: italic;'>$1</span>").
var feedbackString = "<p id='feedbackTxt'>" + 
  currentQuizNode.find("Feedback").text().
    replace(/"(.*)"/, "<span style='font-style: italic;'>$1</span>").
    replace(/(\bat\b.*?\.)/, "<span style='font-weight: bold;'>$1</span>") + 
    "</p>";
$("#quizFeedback").append(feedbackString);

If you meant to say parenthesis instead of quotation marks then change:

replace(/"(.*)"/, "<span style='font-style: italic;'>$1</span>").

To:

replace(/(\(.*?\))/, "<span style='font-style: italic;'>$1</span>").
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文