作者:皮蓬 | 来源:互联网 | 2023-01-13 17:56
我有一个伸手可及的距离文本框,用户可以在其中设计一个html页面,并且我无法访问TextArea的事件输入或输入,因此我无法在运行时更改输入的内容,因此用户将无法输入一个包含各种h
我有一个伸手可及的距离文本框,用户可以在其中设计一个html页面,并且我无法访问TextArea的事件输入或输入,因此我无法在运行时更改输入的内容,因此用户将无法输入一个包含各种html标记的到达文本值,并且在到达文本值中允许使用任何html标记.现在,我想将用户在文本中输入的任何数字更改为word,这意味着如果用户输入1,我希望它成为单词(一个)和2,3,4 ..等等,但是与此同时,我想保持html标记中的数字不变,以便保持用户对他设计的覆盖范围文字的样式,现在例如,如果我有以下生成的html:
Title1: Hi iam first titleTitle3 hi iam third title
iam a div with 23 pixels width
那只是一个示例,但是用户可以构造任何html设计,样式和标签,因此输入内容可能会与此示例不同并且更加复杂.
使用Javascript,我想将其更改为:
Titleone: Hi iam first titleTitlethree hi iam third title
\
iam a div with twothree pixels width
var oldValue = 'Title1: Hi iam first titleTitle3 hi iam third title
iam a div with 23 pixels width
';
var newValue = oldValue.replace(/1|2|3/g, function convertNumbers(x) {
switch (x) {
case '1':
return 'one';
break;
case '2':
return 'two';
break;
case '3':
return 'three';
break;
}
});
但是这段代码的结果
Titleone: Hi iam first titleTitlethree hi iam third title
iam a div with twothree pixels width
我试图用RegularExpressions仅替换任何之间的字符串
(>和(<),但不知道如何构造正则表达式,
请帮忙.
现在我想指定一个仅替换html中的文本并且不更改html标记的样式或属性中的数字的模式,并且我认为可以通过使用正则表达式查找模式来完成ly带有’>’的文本在左侧,“<”在右侧,例如:
Title1: Hi iam first title
如果我通过获取具有’>’的字符串将模式应用于上一个字符串在左侧,“<”在右边,我只会得到“ Title1:嗨,我是第一个标题”,所以我将替换掉然后在此结果字符串中找到的数字以得到我想要的输出.
有可能吗,或者我必须重新考虑使用正则表达式并找到另一种方式来完成任务?
解决方法:
您可以使用jQuery text(function)
方法来更新元素的innerText.
// To store the string representation of the digits
var num = [undefined ,'one', 'two', 'three'];
// Iterate over all the ``, ``
$('h1, h3').text(function(i, text) {
// Match 1, 2 or 3. Regex can also be written as `[123]` or `[1-3]`
return text.replace(/1|2|3/g, function(number) {
return num[number]; // Replace by the textual representation.
});
});
Title1Title3