-3

Using the code [A-Z]+[.] will select All-Caps words ending with a period. I was wondering how I would make this include an all-caps word behind it as well.

bold means it is the selected text

current: ASD ASD.

goal: ASD ASD.

blhsing
  • 91,368
  • 6
  • 71
  • 106
  • `[A-Z ]+[.]` Just add `[ ]` to the character class. Or, `[A-Z\s]+[.]` but that will match vertical spaces (`\n`, `\r`) too – dawg Sep 21 '18 at 01:15
  • If you want to preclude the case of a leading white space, `/[A-Z][A-Z ]+[.]/` – sideroxylon Sep 21 '18 at 01:23

2 Answers2

0
/[A-Z ]+[.]/ 

Will do what you describe.

Demo

You can do [A-Z\s]+[.] but that matches \n too.

Demo 2

dawg
  • 98,345
  • 23
  • 131
  • 206
0

If you want to match either one or two words but no more you can make the second word optional with the ? modifier:

(?:[A-Z]+\s+)?[A-Z]+\.

Or if you want to match as many words as there are before a period, you can use the * modifier instead:

(?:[A-Z]+\s+)*[A-Z]+\.

To obtain the index of the start of a match in JavsScript, you can do the following:

var regex = /(?:[A-Z]+\s+)?[A-Z]+\./;
var match = regex.exec('ASD ASD.');
console.log('The index of the first match is ' + match.index)
blhsing
  • 91,368
  • 6
  • 71
  • 106