-1

I have a string and need to remove all '#' symbols, but only in case when they are placed at the beginning of the word. For example having next string:

"#quick ##brown f#x"

Should be transformed into:

"quick brown f#x"

How it could be achieved using regular expressions in javascript? Thank you in advance for help

Wolf Larsen
  • 105
  • 6
  • What is the beginning of a word here? Start of string/whitespace? – Wiktor Stribiżew Apr 18 '17 at 13:28
  • Did you try searching? I ask because this has been covered many times in many different ways. For example, http://stackoverflow.com/questions/4564414/delete-first-character-of-a-string-in-javascript – Waxi Apr 18 '17 at 13:30

3 Answers3

3
var a = "#quick ##brown f#x"

var e = /(^| )#+/g
a.replace(e, "$1")

That should do the trick.

blue112
  • 52,634
  • 3
  • 45
  • 54
2

use like this (\s+)\#+|^# .It will be prevent middle of the #

Demo

console.log("#quick ##brown f#x".replace(/(\s+)\#+|^#/g,"$1"))
prasanth
  • 22,145
  • 4
  • 29
  • 53
0

You could either split the string and replace every hash # sign that is at the beginning of the string.

var str = "#quick ##brown f#x",
    res = str.split(' ').map(v => v.replace(/^#+/, '')).join(' ');
    
    console.log(res);
kind user
  • 40,029
  • 7
  • 67
  • 77