如何使用正则表达式移除段落开头的多个Hashtag?(基于JavaScript的无代码平台replaceRegex函数应用)
Let's sort out this regex issue so you only strip hashtags from the start of your paragraph, keeping the ones in the middle intact.
The Problem with Your Current Regex
Your existing regex /(?<! \w\s+)(#[a-zA-Z]+ *)/g is too broad—it's matching hashtags anywhere that don't have a "space + word + spaces" before them. That's why your middle #ads is getting removed (since it comes after a period and space, which doesn't meet the negative lookbehind condition).
The Corrected Regex
You need to specifically target only hashtags at the very start of the string, in a continuous block. Use this regex instead:
{{replaceRegex var.a '/^(\#[a-zA-Z]+\s*)+/g' ""}}
How This Works
Let's break down the regex:
^: Anchors the match to the start of the string—so we only look for hashtags right at the beginning.(\#[a-zA-Z]+\s*)+: Matches one or more consecutive leading hashtags:\#[a-zA-Z]+: Matches a single hashtag (starting with#, followed by one or more letters)\s*: Matches zero or more spaces after the hashtag (handles cases where there's a space between leading hashtags, or a trailing space before the main text)- The outer
+ensures we catch all consecutive leading hashtags (like#abc #defin your example)
Result
Applying this to your sample text:
Original:
#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.
After replacement:This is a test paragraph. #ads I only want to remove the hashtags at the beginning and keep the hashtag at the center.
Perfect—this keeps your middle #ads exactly as you wanted!
内容的提问来源于stack exchange,提问作者Shibin




