amedina
amedina

Reputation: 3426

RegEx: Match a word with exactly one occurrence of a specified character at the beginning

I want to detect words starting with $ but ignore words starting with $$ because I want to give the user a way to escape that character.

I have tried many things, but the nearer I got was this: [^\$]\$\w+

It matches occurrences like The side bar $$includes a| $Cheatsheet|, full with the white space at the beginning of the word $Cheatsheet included. It should match the word $Cheatsheet only, without the space.

How can I do it? Any ideas?

Upvotes: 0

Views: 2190

Answers (3)

The fourth bird
The fourth bird

Reputation: 163237

The regex you tried [^\$]\$\w+ will match not a dollar sign followed by a dollar sign and one or more times a word character. That would match for example a$Cheatsheet or $Cheatsheet with a leading space. Note that you don't have to escape the dollar sign in the character class.

If negative lookbehinds are supported, to match a word that does not start with a dollar sign you could use:

(?<!\$)\$\w+

Regex demo

Without a lookbehind you could match what you don't want and capture what you do want in a capturing group.

\${2}\w+|(\$\w+)

Regex demo

If the dollar sign can also not be in the middle of the word you could use:

\S(?:\$+\w+)+\$?|(\$\w+)

Regex demo

Upvotes: 3

Aprillion
Aprillion

Reputation: 22324

Since negative lookbehinds are NOT yet supported in all JavaScript engines (https://github.com/tc39/proposal-regexp-lookbehind), you can start with your regex and introduce a match group:

[^\$](\$\w+)

then, to exclude aaa$bbb, it is possible to use:

\s(\$\w+)

edit: and to match at the beginning or after punctuation:

(?:^|[^$\w])(\$\w+)

https://regex101.com/r/3cW5oY/2

Upvotes: 1

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626748

You want to escape a $ with $. That means, you need

/(?:^|[^$])(?:\${2})*\B(\$\w+)/g

See the regex demo.

Details

  • (?:^|[^$]) - start of string or any char but $
  • (?:\${2})* - 0 or more repetitions of double dollar (this is required to avoid matching literal dollars)
  • \B - requires start of string or non-word char before the next $
  • (\$\w+) - Group 1: a $ and then 1+ word chars.

JS demo:

var s = "The $side bar $$includes a| $Cheatsheet|, $$$$full aaa$side";
var res = [], m;
var rx = /(?:^|[^$])(?:\${2})*\B(\$\w+)/g;
while(m=rx.exec(s)) {
  res.push(m[1]);
}
console.log(res);

Upvotes: 0

Related Questions