-1

i ask a question yesterday. how to remove the tag name only, and remove the tag name including the content yesterday using 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. 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? 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());
});
5
  • 2
    If you're having trouble figuring out what the regex matches use this site regexr.com, I use it all the time when I need to use regex for something it has references as well as a live playground where you can test regex patterns Commented Jul 19, 2016 at 1:30
  • can you explain to me this part sir? var regex = new RegExp(pattern, 'gi'); return str.replace(regex, '').replace(/^\s*[\r\n]/gm, ''); Commented Jul 19, 2016 at 1:34
  • The first line creates a new regular expression with a pattern and the g and i flags, the last one uses a replace from the matched pattern Commented Jul 19, 2016 at 1:36
  • 2
    Could also do all of this with jQuery and no regex Commented Jul 19, 2016 at 1:37
  • Or since you are wanting to do this in C# use the System.Xml namespace to manipulate the xml Commented Jul 19, 2016 at 1:40

2 Answers 2

4

The regex: 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>

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

hello world

The function itself will return the string within the node.

More info can be found here about Regular Expressions.

Sign up to request clarification or add additional context in comments.

Comments

2

For what it's worth here's a solution done using only jQuery.

You could do something similar with a server side XML parser

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.