I have the following:
<span class="hello" id="something somemore"> blah blah blah </span>
My regex is not my strongest ability, would appreciate is someone could help me in javascript regex.
I would like to remove everything/anything and including from <span to > and then finally just remove the </span>.
I am using .replace(myregexhere, ""); to replace other items but I am struggling to do the above.
Also if there are easier or better alternatives let me know.
If you really want to use regex replace, try this:
var newstring = oldstring.replace(/<span[^>]*>([^<]*)<\/span>/, "$1");
Note that this doesn't work if any html tags are in between <span> and </span>. If you have html tags there then please use something like this instead.
You want something like:
~<span.+?>~
and
~</span>~
Why not use replaceWith() instead :
var something = $('#something').text();
$('#something').replaceWith(something);
use jQuery:
$('span').remove();
or for each span with class "hello":
$('span .hello').remove();
or for each element with id "something somemore",
$("#something somemore").remove();
Related
I have this html code:
<p>Hello, this is a test replacing, <span class="myclass">over test</span> and <span class="myclass">over test</span>.</p>
My javascript works to replace the word "Hello" with "FuuBar".
document.body.innerHTML = document.body.innerHTML.replace(/Hello/g, "FuuBar");
But I can not replace <span class="myclass">over test</span> by <span class="thanks"><b>Thanks God</b></span>
I am starting in javascript. I need to resolve this in pure js. Could help in my code? And sorry for the English.
JsFiddle for help.
Use the DOM for this. Check here for more info: https://developer.mozilla.org/en-US/docs/DOM
var spans = document.querySelectorAll('.myclass');
for (var i=0; i<spans.length; i++) {
spans[i].classList.remove('myclass');
spans[i].classList.add('thanks');
spans[i].innerHTML = '<b>Thanks god</b>';
}
Have you checked quotations? You should escape them in the classname in JS or use single quotes. Please provide the code if that's not the case.
Other than that, replacing the whole body doesn't seem the best idea for a task like this.
I want to sub string and remove the , which appears within the span tag and display the name alone. Below are the two cases which needs to work.
Case1: <span class="datatableheader">No results found, </span>
Case2: <span class="datatableheader">Jude Gomes, </span>
A single function should help in removing the , in both cases and display the result as
<span class="datatableheader">No results found </span>
<span class="datatableheader">Jude Gomes </span>
Appreciate for any help.
Thanks
$(".datatableheader").html ($(".datatableheader").html().replace(",",""));
It's not widely recognized that .html accepts a callback function:
$('.datatableheader').html(function(i,old) {
return old.replace(/, ?/g, '');
});
http://jsfiddle.net/3fBY4/1/
you can try this also
var parts = id.split(':'); //because u have case1: or case2:
// it will split into string in array//
$('#parts[1]').replace(",",""));
//try to print that it will work
nice question.
I want to remove the end tag of a given HTML string. I get the HTML string from the document like so:
var front = $('<div>').append($(clean_copy).contents().remove().end()).remove().html();
I now want to remove the </div> from the below:
<div id="div1" style="font-size: 20px"></div>
I am really rusty with regex but I want to try something like this:
console.log("Remove End: " + front.replace('/<(/)?div([^>]*)>/g', ''));
The above doesn't work, how can I remove any tag? Also some strings may not have an end tag.
try front.replace(/<\/div>$/)
It replaces your div.
If you like to replace any last end tag use this front.replace(/<\/\S+>$/)
How can I "clean away" all but the word dog from the html code below using Javascript
and perhaps regex? The id will vary.
<div class="tag" id="11">dog</div>
although its very bad idea to parse html via js , but if you want then try this
<.*>(.*)<\/.*>
DEMO
ids should not begin with a digit.
<div class="tag" id="d11">dog</div>
var who= document.getElementById('d11');
alert(who.textContent || who.innerText || '');
If it's always going to be class=tag, then a bit of jquery can help with this:
$('.tag').html()
If it's in a string already
var s = '<div class="tag" id="11">dog</div>';
$(s,'.tag').html()
In the string
some text <p id='item_1' class='item'>multiline content\r\n\r\n for <br/>remove</p><br clear='all' id='end_of_item_1'/><p id='item_2' class='item'>another multiline content\r\n\r\n</p><br clear='all' id='end_of_item_2'/>
I need to remove
<p id='item_1' class='item'>multiline content\r\n\r\n for <br/>remove</p><br clear='all' id='end_of_item_1'/>
Can't find a way how to do it.
var id = 'item_1';
var patt=new RegExp("<p id='"+id+"'(.)*|([\S\s]*?)end_of_"+id+"'\/>","g");
var str="some text <p id='item_1' class='item'>multiline content\r\n\r\n for <br/>remove</p><br clear='all' id='end_of_item_1'/><p id='item_2' class='item'>another multiline content\r\n\r\n</p><br clear='all' id='end_of_item_2'/>";
document.write(str.replace(patt,""));
The result is
some text for
<br>
remove
<p></p>
<br id="<p id=" class="item" clear="all" item_2'="">
another multiline content
<p></p>
<br id="end_of_item_2" clear="all">
Please help to solve this.
Here's the regex for the current scenario. When the regex approach eventually breaks, remember that we warned that parsing HTML with regex was a fool's errand. ;)
This:
var s = "some text <p id='item_1' class='item'>multiline content\r\n\r\n for <br/>remove</p><br clear='all' id='end_of_item_1'/><p id='item_2' class='item'>another multiline content\r\n\r\n</p><br clear='all' id='end_of_item_2'/><ul><li>";
var id = 'item_1';
var patt = new RegExp ("<p[^<>]*\\sid=['\"]" + id + "['\"](?:.|\\n|\\r)*<br[^<>]*\\sid=['\"]end_of_" + id + "['\"][^<>]*>", "ig")
var stripped = s.replace (patt, "");
Produces this:
"some text <p id='item_2' class='item'>another multiline content
</p><br clear='all' id='end_of_item_2'/><ul><li>"
Why can't you use the DOM API to remove it? (add everything to the document, and then remove what you don't need)
var item1 = document.getElementById('item_1'),
endOfItem1 = document.getElementById('end_of_item_1');
item1.parentNode.removeChild(item1);
endOfItem1.parentNode.removeChild(endOfItem1);
I need to assume a bit of unspoken constraints from your question, to get this to work:
Am I right in guessing, that you want a regex, that can find (and then replace) any 'p' tag with a specific id, up to a certain tag (like e.g. a 'br' tag) with an id of 'end_of_[firstid]'?
If that is correct, than the following regex might work for you. It may be, that you need to modify it a bit, to get JS to accept it:
<p\s+id='([a-zA-Z0-9_]+)'.*?id='end_of_\1'\s*\/>
This will give you any constellation with the criteria, describled above, and the name if the id as group 1, It should now be a simple task, to check if group1 contains the id you want to remove and then replace the whole match with an empty string.
If I understand your example correcty (I am not that good with JavaScript and my RegEx was based rather on the general perl-regex fashion) you could maybe do something like the following:
var patt=new RegExp("<p\s+id='"+id+"'.*?id='end_of_"+id+"'\s*\/>","g");
That way, you don't have to worry about group matching, although I find it to be more elegant, to match the id you wanted via a group instead of inserting it into the RegEx.