-2

The SO link Javascript Regular expression to remove unwanted <br>, &nbsp; sp tells us to remove an expression using regex.But i want to remove &nb sp; without using regex.Angular expression:

{{notification.noti_val)}};Did:{{notification.noti_val.replace(/\&nb sp;/g, ' ')}}

.But doesnt delete the &nb sp;

Note:did a space between nb and sp cause SO was parsing it as whitespace so was showing it as whitespace

Community
  • 1
  • 1
WebInsight
  • 307
  • 2
  • 20
  • If it worked with regular javascript why not use regular JS instead of using the angular library? – Daniel Kobe Apr 16 '16 at 06:57
  • If i go to that link it tells us that parsing html with regex is not a good idea.The link ,which is in that page,is http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – WebInsight Apr 16 '16 at 06:59
  • *"But i want to remove &nb sp; without using regex"* Why? Calling `replace` with a string instead of a regex in the first argument only replaces the **first** match. In any case, you'll need to give us (much) more to work with, such as a [mcve]. – T.J. Crowder Apr 16 '16 at 06:59
  • check the 2nd answer on the link you posted in your question – Daniel Kobe Apr 16 '16 at 07:01
  • @kobe : have used regular js method 'replace';It is not part of angular library – WebInsight Apr 16 '16 at 07:22
  • Why the downvote people?I have my own reasons to not use regex. – WebInsight Apr 16 '16 at 08:39

1 Answers1

1

Use \s* instead of a literal space character. \s should match any kind of whitespace character. Adding * next to \s should match any kind of space zero or more times.

noti_val.replace(/\&nb\s*sp;/g, ' ')

Note that this would replace &nbsp; with a space character. If you want to remove then replace it with empty string.

noti_val.replace(/\&nb\s*sp;/g, '')

DEMO

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • Avinash will your code work for "&nbsp"?I only gave the space between them cause SO was parsing it as whitespace.Sorry for such a noob in regex – WebInsight Apr 16 '16 at 07:06
  • Ok thanks for your time man but an error is showing.exer Error: Unexpected next character at columns 31-31 [&] in expression [notification.noti_val.replace(/&nb\s*sp;/g, ' ')]. – WebInsight Apr 16 '16 at 07:11
  • is that angular should treat `&` as special char? If yes then escape that. – Avinash Raj Apr 16 '16 at 07:15
  • Did not work.The error showing now is : `Error: [$parse:lexerr] Lexer Error: Unexpected next character at columns 31-31 [\] in expression [notification.noti_val.replace(/\&nb\s*sp;/g, ' ')].'The character in the [] is \.SO is not showing it – WebInsight Apr 16 '16 at 07:18
  • https://jsfiddle.net/U3pVM/24152/ Please follow this link.And thanks for your time on this – WebInsight Apr 16 '16 at 07:29
  • You got me.But why wasnt it working on the template side?Can you tell.Any ways thanks for the time.and i will accept your answer – WebInsight Apr 16 '16 at 07:36
  • 1
    I'm also new to angular. I didn't find an example which uses js in the template side.. Is there any one? Using the above code, you may also define filters. Ex http://stackoverflow.com/a/36452649/3297613 – Avinash Raj Apr 16 '16 at 07:37