-2

I want to remove character from a string ex: [\"lorem ipsum\"] , i want like lorem ipsum without this characters [\" \"].

how i can do this in javascript or PHP ?

Mahmoud Ismail
  • 1,617
  • 4
  • 27
  • 51

2 Answers2

2

In javascript

var str = "[\"lorem ipsum\"]";
alert(str.replace(/[^a-zA-Z ]/g, ""));

And in PHP

$string = "[\"lorem ipsum\"]";
$result = preg_replace('/[^A-Za-z0-9\-]/', ' ', $string);
echo $result;
0

JavaScript

If you want to remove this specific combination [\" \"] just use the replace function:

mystring = mystring.replace('[\"','');
mystring = mystring.replace('\"]','');

If you want to remove all non-alphanumeric chars:

The following is the/a correct regex to strip non-alphanumeric chars from an input string:

mystring = mystring.replace(/\W/g, '')

Note that \W is the equivalent of [^0-9a-zA-Z_] - it includes the underscore character. To also remove underscores use e.g.:

mystring = mystring.replace(/[^0-9a-z]/gi, '')
user3378165
  • 6,546
  • 17
  • 62
  • 101