简体   繁体   English

javascript:有人可以解释代码吗

[英]javascript: can someone explain the code

i ask a question yesterday. 我昨天问一个问题。 how to remove the tag name only, and remove the tag name including the content yesterday using jquery. 如何仅删除标签名称,以及如何使用jquery删除昨天包含内容的标签名称。

and the answer is using regex. 答案是使用正则表达式。

can someone explain the code below for me just the regex part? 有人可以仅正则表达式部分为我解释以下代码吗?

i already read some articles about regex but i think i dont fully understand it clearly. 我已经读过一些有关正则表达式的文章,但我想我还不太清楚。

if possible, i want to convert it to c# code, but i can't do it yet because i dont fully understand the code. 如果可能的话,我想将其转换为C#代码,但是我还不能做,因为我不完全了解该代码。 thank you 谢谢

first code 第一个代码

$(function() {
  function removeNode(str, nodeName) {
    var pattern = '<'+nodeName+'>[\\s\\w]+<\/'+nodeName+'>';
    var regex = new RegExp(pattern, 'gi');
    return str.replace(regex, '').replace(/^\s*[\r\n]/gm, '');
  }

what i dont undertand in the first code is where does the 'gi' come from? 我在第一个代码中不了解的是“ gi”是从哪里来的? and the return 和回报

var regex = new RegExp(pattern, 'gi');
        return str.replace(regex, '').replace(/^\s*[\r\n]/gm, '');

second code 第二码

$(function() {
  function removeNodeButRetain(str, nodeName) {
    var pattern = '<\/?'+nodeName+'>';
    var regex = new RegExp(pattern, 'gi');
    return str.replace(regex, '').replace(/^\s*[\r\n]/gm, '');
  }

  what i dont understand is the 

 var regex = new RegExp(pattern, 'gi');
        return str.replace(regex, '').replace(/^\s*[\r\n]/gm, '');

third code 第三码

jQuery(document).ready(function(){
textval = $('textarea').val();
textnewval = textval.replace('Para TextBreak="No"', 'p').replace('/Para', '/p'); 

  if(textnewval.indexOf('Italic') >= 0) //If Italic
{
    EmphasisAttr = 'Italic';
  textnewval = textnewval.replace('Emphasis Type="'+EmphasisAttr+'"', 'i').replace('/Emphasis', '/i'); 
}
if(textnewval.indexOf('Bold') >= 0) //If Bold
{
    EmphasisAttr = 'Bold';
  textnewval = textnewval.replace('Emphasis Type="'+EmphasisAttr+'"', 'b').replace('/Emphasis', '/b'); 
}
if(textnewval.indexOf('Underline') >= 0) //If underline
{
    EmphasisAttr = 'Underline';
    textnewval = textnewval.replace('Emphasis Type="'+EmphasisAttr+'"', 'u').replace('/Emphasis', '/u'); 
}


  $('textarea').val(textnewval);
  alert($('textarea').val());
});

The regex: http://regexr.com/3dr56 正则表达式: http//regexr.com/3dr56

Will match any nodes that consist of say the following: 将匹配任何包含以下内容的节点:

<div>hello world</div>

Provided that the parameters passed to the function: 假设将参数传递给函数:

 function removeNode(str, nodeName) { var pattern = '<'+nodeName+'>[\\\\s\\\\w]+<\\/'+nodeName+'>'; var regex = new RegExp(pattern, 'gi'); return str.replace(regex, '').replace(/^\\s*[\\r\\n]/gm, ''); } console.log("Node: <div>hello world</div>"); var str = removeNode("hello world", "div"); console.log("String returned: " + str); 

are: 是:

Node match: <div>hello world</div> 节点匹配: <div>hello world</div>

removeNode("hello world", "div"); will return: 将返回:

hello world

The function itself will return the string within the node. 函数本身将返回节点内的string

More info can be found here about Regular Expressions . 在此处可以找到有关正则表达式的更多信息。

For what it's worth here's a solution done using only jQuery. 值得的是,这里有一个仅使用jQuery完成的解决方案。

You could do something similar with a server side XML parser 您可以使用服务器端XML解析器执行类似的操作

Data used for search and replace: 用于搜索和替换的数据:

  // selector = jQuery selector, final = replacement element
  var tags = [{
      selector: 'emphasis[Type="Italic"]',
      final: '<i>'
    }, {
      selector: 'emphasis[Type="Bold"]',
      final: '<strong>'// note <b> is deprecated
    }, {
      selector: 'emphasis[Type="Underline"]',
      final: '<u>'
    }, {
      selector: 'para',
      final: '<p>'
    }
  ];

Parser: 解析器:

$(function() {

  // get string to parse
  var str = $('#txtArea').val();
  // inject in temporary div
  var $tempdiv = $('<div>').html(str);
  // loop over all the tags to replace
  $.each(tags, function(_, tag) {
    // loop and replace instances of tags
    $tempdiv.find(tag.selector).replaceWith(function() {
      return $(tag.final).html( $(this).html())
    });
  });
  // get final string from temp div
  var finalString = $tempdiv.html()
  // update textarea
  $('#final').val(finalString );
});

DEMO 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM