-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

asked Apr 18, 2017 at 13:27
2
  • What is the beginning of a word here? Start of string/whitespace? Commented 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/… Commented Apr 18, 2017 at 13:30

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

Comments

2

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

Demo

console.log("#quick ##brown f#x".replace(/(\s+)\#+|^#/g,"1ドル"))

answered Apr 18, 2017 at 13:32

1 Comment

The output is incorrect. 'quickbrown f#x' !== 'quick brown f#x'
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);

answered Apr 18, 2017 at 13:35

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.