1

So I am trying to select some names with JS but I can figure out how. I found 3 solution here but still could not get it to work:

I would like to select word that DOESN'T start with . and HAS to end with {

Here is what I have: \b(?!\.)[\w\-]+(?=\s*{)\b

Also tried: ^(?!\.)[\w\-]+(?=\s*:)

Example:

.test { }

test { } <--- Select this test

Maverick
  • 876
  • 2
  • 12
  • 22

4 Answers4

3

If you wish to match -^!foo {}, use (?:\s|^)([^\s\.]+(?=\s*\{)).

If you wish to only match foo {}, use (?:[^\w\.]|^)([^\W\.]+(?=\s*\{)).

var pattern1 = /(?:\s|^)([^\s\.]+(?=\s*\{))/gm,
    pattern2 = /(?:[^\w\.]|^)([^\W\.]+(?=\s*\{))/gm,
    text = ".foo{}  bar {} !!baz{} ..-boom  {}",
    match;

console.log('First pattern:');
while (match = pattern1.exec(text)) {
  console.log(match[1]); // Prints "bar", "!!baz"
}

console.log('Second pattern:');
while (match = pattern2.exec(text)) {
  console.log(match[1]); // Prints "bar", "baz", "boom"
}

Explanation of the first regex:

  1. We expect the leading position before your word to either be the start of the line ^ or whitespace \s.

  2. The word itself consists of repeated non-whitespace characters that are not dots [^\s\.].

  3. The word must be followed by a {, for which we use lookahead via (?=\{).

JavaScript's regex engine doesn't support lookbehind, so you have to use a non-capturing group (?:...) to match the leading position before your word.

See JavaScript regular expressions and sub-matches for an explanation of how to access capturing groups

See https://regex101.com/r/bT8sE5/5 for a live demo of the regex with further explanation.

Community
  • 1
  • 1
le_m
  • 19,302
  • 9
  • 64
  • 74
  • This is what I was looking for... Can you please explain how you did it, because as I can see you negated the . – Maverick Jun 07 '16 at 22:42
  • Thank you for the detail explanation. Can you please make a simple edit here in comment how would I make an exaction. For example if I find a keyword after `{` don't select it. `test { keyword` this should not be selected. I already know the key word.. It's keywords buy nevermind.. What I tried `^(?!.*keyword$)([^\S\.\#]|^)([^\s.\#(]+(?=\{))+$` – Maverick Jun 07 '16 at 23:20
  • 1
    so you want `test{}` to match, but `test{keyword}` should not match? Then append `(?!\{keyword)` to the first regex in this answer (a negative lookahead). If you also would like to match `test {}` but not `test { keyword` (note the spaces) use `(?:\s|^)([^\s\.]+(?=\s*\{))(?!\s*\{\s*keyword)` – le_m Jun 07 '16 at 23:22
  • Right, when there is a special keyword after `{` exclude it. `class{ignore }` not selecter – Maverick Jun 07 '16 at 23:26
0

How about this:

([^\w\.]|^)(\w+\{)

It's basically saying anything at the start of the line, or beginning with a nonword / nondot character.

It's tricky to do with \b since it matches after the dot quite happily. You can possibly get it to work with the negative lookahead but it's pretty funky stuff at this point :)

Matthew
  • 10,361
  • 5
  • 42
  • 54
0

You can do it with this: ^.*\.(\w+\{\}){1}.*$

Explanation:

  • ^ is the beginning of the string
  • .* matches everything behind the dot (.)
  • (\w+\{\})* the capture group matches the word and the brackets after it (for example test{}} zero or more times
  • .* matches everything after the word
  • $ is the end of the string

So for the input: sadasdas.test{}daasdasdasdasd it will match test{}

Try it out here: https://regex101.com/r/hE4uY4/1

Andras Szell
  • 527
  • 2
  • 13
  • Nice try but I think you miss understood me, I want to select only `test` without a dot. Basically with a string has a dot in front ignore it, but if it doesn't check if it ends with `:` – Maverick Jun 07 '16 at 22:48
0

The following works in relation to http://regexr.com/ You can test it there.

/(?![\s])(^[^.]([\S]+)[{}][\s])/igm
Rhys Tabor
  • 21
  • 2