0

i am getting a json in return whose value is &<>" and many more special character.I want a common solution to convert all of them into their actual values like &amp as &,&quot into ",,etc. I dont want to use anthing like the below code because in future more different symbols can come-

    $scope.oldNotification[i].shorttext=$scope.oldNotification[i].shorttext.replace(/&amp;/g, '&');
    $scope.oldNotification[i].shorttext=$scope.oldNotification[i].shorttext.replace(/&lt;/g, '<');
    $scope.oldNotification[i].shorttext=$scope.oldNotification[i].shorttext.replace(/&gt;/g, '>');
    $scope.oldNotification[i].shorttext=$scope.oldNotification[i].shorttext.replace(/&quot;/g, '"');
    $scope.oldNotification[i].shorttext=$scope.oldNotification[i].shorttext.replace(/&#039;/g, "'");

Is there any api to handle these value and convert them into their actual value? I am using angularJs.

Yogeshree Koyani
  • 1,649
  • 10
  • 19
RAHUL DEEP
  • 695
  • 3
  • 13
  • 33

2 Answers2

0

Since you're using angular, you can use jQuery.

Just create an element with jQuery, and then parse it using its text() property

var str = $( "<span>&quotDale ca&ntilde;a&quot&#169;</span>" ).text();
console.log(str);

The output is "Dale caña"©

You will need to put your string within a valid html tag, such as '<span>'+yourtext+'</span>'

Fiddle demo

click on "run" and open your browser's console

Community
  • 1
  • 1
Luis Sieira
  • 29,926
  • 3
  • 31
  • 53
0

Got the answer...working for all the characters(which i checked)...

$scope.oldNotification[i].shorttext=$('<textarea />').html($scope.oldNotification[i].shorttext).text();
RAHUL DEEP
  • 695
  • 3
  • 13
  • 33