How to remove multiple hashtags from the start of a paragraph using regex?

Viewed 95

I want to remove multiple hashtags from the beginning of a paragraph.

#abc #def This is a test paragraph. #ads I only want to remove the first hashtags and keep the rest.

How should I remove the hashtags at the beginning of the paragraph?

I'm using a no-code platform to work. So there are limitations for me to write code. But I'm using the replaceRegex function for doing this. The platform works on javascript.

What I have

var.a = "#abc #def This is a test paragraph. #ads I only want to remove the hashtags at the beginning and keep the hashtag at the center.

Function

{{replaceRegex var.a '/(?<! \w\s+)(#[a-zA-Z]+ *)/g' " "}}

Actual Result

   This is a test paragraph.  I only want to remove the hashtags at the beginning and keep the hashtag at the center.

Expected Result

 This is a test paragraph. #ads I only want to remove the hashtags at the beginning and keep the hashtag at the center.
2 Answers

You could use

^#[a-zA-Z]+(?: +#[a-zA-Z]+)* *
  • ^ Start of string
  • #[a-zA-Z]+ Match # and 1+ chars a-zA-Z
  • (?: +#[a-zA-Z]+)* Optionally repeat 1+ spaces, # and 1+ chars A-Z
  • * match optional spaces

If you want to match whitespaces you can use \s but note that it can also match a newline.

regex demo

You can replace the match with an empty string.

For example using Javascript:

const regex = /^#[a-zA-Z]+(?: +#[a-zA-Z]+)* */gm;
const str = `#abc #def This is a test paragraph. #ads I only want to remove the hashtags at the beginning and keep the hashtag at the center.`;
console.log(str.replace(regex, ''));

You can use

^(?:\s*#[a-zA-Z]+)+\s*

See this regex demo.

NOTE: If there can be any Unicode letters and you are using an ECMAScript 2018+ compliant JavaScript environment, you might use

/^(?:\s*#\p{L}+)+\s*/u

where \p{L} matches any Unicode letter.

Details:

  • ^ - start of string
  • (?:\s*#[a-zA-Z]+)+ - one or more repetitions of
    • \s* - zero or more whitespaces
    • #[a-zA-Z]+ - # and one or more ASCII letters
  • \s* - zero or more whitespaces
Related