【问题标题】:replacing string value and keep part of the replace string using javascript使用javascript替换字符串值并保留部分替换字符串
【发布时间】:2016-07-07 17:15:00
【问题描述】:

我有一个到达文本框,用户可以在其中设计一个 html 页面,我无法访问 TextArea 的事件 keyup 或 keydown,所以我无法在运行时更改用户输入数字的输入,所以我会最后得到一个包含各种html标签的reachtext值,并且在reachtext值中允许任何html标签,现在我想将用户在文本中输入的任何数字更改为word,这意味着如果用户输入1我想要它是单词(一)和 2,3,4.. 等,但同时我想保留在 html 标签中找到的数字,不做任何更改,以保持用户对到达文本所做的样式他设计,现在例如,如果我有以下生成的 html:

<h1>Title1: Hi iam first title</h1><h3>Title3 hi iam third title</h3>
<div style='width:23px'>iam a div with 23 pixels width</div>

这只是一个示例,但用户可以构建任何 html 设计、样式和标签,因此输入可能会有所不同并且比此示例更复杂。 使用 javascript 我想将其更改为:

<h1>Titleone: Hi iam first title</h1><h3>Titlethree hi iam third title</h3>\
<div style='width:23px'>iam a div with twothree pixels width</div>

var oldValue = '<h1>Title1: Hi iam first title</h1><h3>Title3 hi iam third title</h3>
<div style='width:23px'>iam a div with 23 pixels width</div>';
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;
    }
});

但是这段代码的结果

<hone>Titleone: Hi iam first title</hone><hthree>Titlethree hi iam third title</hthree>
<div style='width:twothreepx'>iam a div with twothree pixels width</div>

我尝试使用正则表达式仅替换任意之间的字符串 (&gt;) 和 (&lt;) 但不知道如何构造正则表达式, 请帮忙。 现在我想指定一个仅替换 html 中的文本并且不更改 html 标签的样式或属性中的数字的模式,我认为可以通过使用正则表达式找到一个模式来完成就是左边有'>',右边有'

<h1>Title1: Hi iam first title</h1>

如果我通过获取左边有“>”和右边有“

【问题讨论】:

标签: javascript jquery regex replace


【解决方案1】:

如果字符后面没有&gt; 字符,您可以使用RegExp /(1|2|3)(?!&gt;)/g 匹配123

var oldValue = '<h1>Title1</h1><h3>Title3</h3>';
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;
  }
});

document.write(newValue)

【讨论】:

  • 如果 iam 面对的所有标签都以数字后跟 > 字符结尾,但实际上我的案例可以包含任意数量的标签和任何类型,例如
    title3
    我需要一个对所有 html 标签更通用的解决方案
  • @محمدعبدالرحمن “我需要一个对所有 html 标签更通用的解决方案” 原始问题中的示例字符串仅包含 h1h3 标签。您能否更新 Question 以描述您尝试使用实际使用的 html 字符串来实现的目标?
  • @محمدعبدالرحمن 您可以使用 Tushar 发布的解决方案;将数字映射到单词,将数字替换为对象的值
【解决方案2】:

您可以使用jQuery text(function) 方法来更新元素的innerText。

// To store the string representation of the digits
var num = [undefined ,'one', 'two', 'three'];

// Iterate over all the `<h1>`, `<h3>`
$('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.
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>

<h1>Title1</h1><h3>Title3</h3>

【讨论】:

  • 如果我处理的唯一标签是 h1 和 h2,这个解决方案会很好,但是我的 oldvalue 可以包含任何 html 标签,例如
    我需要一个对所有 html 标签更通用的解决方案
  • @محمدعبدالرحمن "但我的 oldvalue 可以包含任何 html 标签,例如
    "
    鉴于问题中的要求描述, &lt;div style='width:3px'&gt;&lt;/div&gt; 不应该受到替换的影响?您应该能够在jQuery() 添加多个选择器,并在num 使用上面当前答案中的模式替换多个数字?
  • @محمدعبدالرحمن 您只需在$('h1, h2') 中添加选择器,或者您甚至可以使用通用选择器$('*')
  • @Tushar 非常感谢你,我认为这会很好,但还有一点我有 html var oldValue ="

    Title1: Hi iam first title

    的字符串>

    Title3 你好,我是第三个标题

    我是一个宽度为 23 像素的 div
    ";我如何动态将其更改为文档元素以在其上应用通用选择器,因为 $('*') 将适用于不是我的目标的当前文档
  • @محمدعبدالرحمن 您可以为所有元素添加一些类并使用$('.myClass') 选择器,或者向它的容器添加一个类并使用$('containerSelector *') 选择器。
猜你喜欢
  • 1970-01-01
  • 2020-03-04
  • 2011-09-04
  • 2021-03-01
  • 2012-07-17
  • 2021-03-25
  • 2021-04-19
  • 1970-01-01
相关资源
最近更新 更多