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
-
What is the beginning of a word here? Start of string/whitespace?Wiktor Stribiżew– Wiktor Stribiżew2017年04月18日 13:28:42 +00:00Commented Apr 18, 2017 at 13:28
-
Did you try searching? I ask because this has been covered many times in many different ways. For example, stackoverflow.com/questions/4564414/…Waxi– Waxi2017年04月18日 13:30:46 +00:00Commented Apr 18, 2017 at 13:30
3 Answers 3
var a = "#quick ##brown f#x"
var e = /(^| )#+/g
a.replace(e, "1ドル")
That should do the trick.
answered Apr 18, 2017 at 13:30
blue112
57.2k3 gold badges52 silver badges58 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
use like this (\s+)\#+|^# .It will be prevent middle of the #
console.log("#quick ##brown f#x".replace(/(\s+)\#+|^#/g,"1ドル"))
answered Apr 18, 2017 at 13:32
prasanth
22.6k4 gold badges33 silver badges57 bronze badges
1 Comment
evolutionxbox
The output is incorrect.
'quickbrown f#x' !== 'quick brown f#x'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);
answered Apr 18, 2017 at 13:35
kind user
42k8 gold badges69 silver badges78 bronze badges
Comments
lang-js