-2

How can I remove all elements from a string apart from raw text and a single space? However, certain symbols are not getting removed, - , –, ’

Currently I use:

let descriptionString = description.replace(/(<([^>]+)>)/gi, "").replace(/[^a-zA-Z 0-9]+/g,"");

description is a field from wordpress which can contain anything, html, symbols, encoded chars etc.

Example content is:

Surgical Sim 8211 Kate O8217Connor Sim Lab I need to remove 8217 - its only removed the hash at the moment.

I need to display the result in a react-native View. I don't want to use a WebViewto render html.

Any ideas?

Bomber
  • 10,195
  • 24
  • 90
  • 167
  • 1
    1. What is `description`? 2. [See here](https://stackoverflow.com/questions/1433212/replace-method-doesnt-work), you're not using the result of the second `replace`. – T.J. Crowder Sep 01 '18 at 11:19
  • Still doesn't remove everything I need to get removed. – Bomber Sep 01 '18 at 11:25
  • Assuming you're running this in a browser, have you considered inserting this as the innerHTML of a div element (not in the DOM) and retrieving the *textContent*? – RobG Sep 01 '18 at 11:27
  • Its for a `react-native` view – Bomber Sep 01 '18 at 11:30
  • Again: What's in `description`? Also see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions – T.J. Crowder Sep 01 '18 at 11:39

1 Answers1

0

Check the below function. It removes all the special charactors, html and symbols.

function filterString(html)
    {
       var tmp = document.createElement("DIV");
       tmp.innerHTML = html;
       tmp = tmp.textContent || tmp.innerText || "";

       tmp =tmp.replace(/([,"'0-9\-.~!@#$%^&*()_+=–’`{}\[\]\|\\:;"<>\/?])+/g, '').replace(/^(-)+|(-)+$/g,'');
       return tmp;
    }

    console.log(filterString("<html>asdasjhd<body>%dasd test - , ' &#8211;, &#8217; ytest jsdnja  @!@#@&^$@$ &nbsp;"));

Also you can test it for your string

console.log(filterString("Surgical Sim 8211 Kate O8217Connor  Sim Lab"));