0

I am trying to replace '<p>hi</p>' with 'hi'

My JavaScript:

var obj = {item:{title: params.title,description: params.text, link: req.headers['origin']+"/blog/blog-description/"+params.blog_id, guid:req.headers['origin']+"/blog/blog-description/"+params.blog_id}};

var xml2js = require('xml2js');     
var builder = new xml2js.Builder();
var xml = builder.buildObject(obj);
var modXml = xml.replace('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>', '');
var modXml = xml.replace(/<(?:.|\n)*?>/gm, '');         
var new_text = modXml;

fs.readFile('public/feed.xml', function read(err, data) {
    if (err) throw err;
    var file_content = data.toString();
    var c = file_content.indexOf('</description>');
    var position = c + 14;
    file_content = file_content.substring(position);
    var file = fs.openSync('public/feed.xml','r+');
    var bufferedText = new Buffer(new_text+file_content);
    fs.writeSync(file, bufferedText, 0, bufferedText.length, position);
    fs.close(file);
});

i didnt know where it went wrong can someone suggest help.

2
  • 1
    So what issue are you running across? I don't see a question here. Commented Aug 22, 2016 at 6:45
  • Instead of replacing the header, just call new xml2js.Builder({headless: true}); Commented Aug 22, 2016 at 7:28

4 Answers 4

2

Try this:

xml.split('<p>hi</p>').join('hi');
Sign up to request clarification or add additional context in comments.

Comments

1

xml2js replaces your <p> tag with &lt;p&gt; (and the </p> with &lt;/p&gt;), so you need to replace the replaced string. Also, call Builder() with the headless: true option - then you don't need to replace the header. The following works:

var obj = {item:{title: '<p>foobarbaz</p>',description: "<p>foo</p>", link: "<p>bar</p>", guid:"<p>baz</p>"}};

var xml2js = require('xml2js');
var builder = new xml2js.Builder({ headless: true });
var xml = builder.buildObject(obj);
var modXml = xml.replace(/&lt;p&gt;|&lt;\/p&gt;/gm, '');
console.log(modXml);

Output:

<item>
  <title>fobaba</title>
  <description>foo</description>
  <link>bar</link>
  <guid>baz</guid>
</item>

4 Comments

Thanks a lot ,,,,,,,
Is it possible to append only first 100 words of my description.
Sure, use substring: str.substring(0, 99)
Thanks,how to replce only </br> tag in xml
1

Since replace is returning a string object, you can simply chain the calls:

Have you tried:

var modXml = xml.replace('<p>', '').replace("</p>",'');

I think you could also condense it into one regular expression.

Comments

1

you could either use a regex to strip tags, or use a library such as striptags

optional regex:

var text = '<p>aaa</p>';
console.log(text.replace(/<(?:.|\n)*?>/gm, ''));

striptags library could be used to strip all tags, or only specific tags:

var text = '<p>aaa</p>';
striptags(html);
striptags(html, ['p']);

5 Comments

Thank you,MoLow but it says replace is not deined
that means your variable is not a string. in your case, try xml.replace(/<(?:.|\n)*?>/gm, '')
still it is not removing i will edit the code pls have a look
I don't see any problem. maybe the html tags you are trying to replace are in file_content, and not in new_text?
Molow,i replaced the step here,but not wrking var file_content = data.toString(); var file_content = file_content.replace(/<(?:.|\n)*?>/gm, ''); var c = file_content.indexOf('</description>');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.