0

I have text like this

a b:_c

a:_c

The regex that I am using is /:(.*)/g , but it also captures ':_'

I want to achieve--> c

What I've got--> :_c

How do I exclude them?

P.S.: replaced space with underscore, for easy understanding.

EDIT: I want to capture everything behind ': '

EDIT2: here is regexr with text, as u can see, it also captures ': '

user1898714
  • 141
  • 1
  • 1
  • 6
  • 2
    A period matches any character, `[a-zA-Z]` would match the alphabet. – adeneo Jul 27 '17 at 18:50
  • It requires a clearer description. Do you want to capture a single character at the end of the line, that is preceded by a colon and space? Or... – Andy G Jul 27 '17 at 18:52
  • Can you post a code example of what you've done so far? – Nathan Jul 27 '17 at 19:03
  • Possible duplicate of [How do you access the matched groups in a JavaScript regular expression?](https://stackoverflow.com/questions/432493/how-do-you-access-the-matched-groups-in-a-javascript-regular-expression) – revo Jul 27 '17 at 19:11
  • 1
    If you care about comments you will find your way. – revo Jul 27 '17 at 19:20
  • @revo i do care bout comments, I'm testing every solutions that have been suggested here – user1898714 Jul 27 '17 at 19:21
  • 1
    Your one and only problem is ignoring *capturing groups* which without them you wouldn't be able to achieve desired result. – revo Jul 27 '17 at 19:24

3 Answers3

0

You can use: /:\s(.*)/g.

That will capture a single space after a : before starting to capture:

a b: c => "c"
a: c   => "c"

var re = /:\s(.*)/;

var tests = [
    'a: c',
    'a: cat',
    'b a: test',
    'a: c',
    'Test: this is the rest of the sentence',
];

for (var i = 0; i < tests.length; i++) {
    console.log('TEST: ', '"' + tests[i] + '"');
    console.log('RESULT: "' + tests[i].match(re)[1] + '"');
}

Note: You must get the first capture group from the match. If you do not, you will not get the desired result. See How do you access the matched groups in a JavaScript regular expression? for information on how to do this.

Sumner Evans
  • 8,951
  • 5
  • 30
  • 47
  • You need to get the first captured group. See my latest edits. – Sumner Evans Jul 27 '17 at 19:00
  • `:\s(.*)` absolutely doesn't provide an answer. – revo Jul 27 '17 at 19:04
  • @revo, it absolutely does answer the question. Note the text of the OP's question: *P.S.: replaced space with dash, for easy understanding.* – Sumner Evans Jul 27 '17 at 19:06
  • It's not a *dash* it's called *underscore*, firstly. Second, I was referring to *...everything after character `:` so result will start from **letters**.* part of requirements which by now is removed / re-phrased by original poster. – revo Jul 27 '17 at 19:09
  • @SumnerEvans i want capture everything behind the :_ , not just single c – user1898714 Jul 27 '17 at 19:10
  • @revo, yes the OP used the wrong terminology. My answer now does capture everything after `: `. – Sumner Evans Jul 27 '17 at 19:11
  • @user1898714, have you tried running the snippet I included in my answer? – Sumner Evans Jul 27 '17 at 19:12
  • @SumnerEvans yes, still not exactly what i needed, sry for wrong requirements :< i want to capture everything behind ": ", till the end of sentence. – user1898714 Jul 27 '17 at 19:15
  • 1
    Your main problem is not knowing what [capturing groups](https://stackoverflow.com/documentation/regex/660/capture-groups#t=201707271903279175358) are and [how one can access them](https://stackoverflow.com/questions/432493/how-do-you-access-the-matched-groups-in-a-javascript-regular-expression). @user1898714 – revo Jul 27 '17 at 19:16
  • @user1898714, I have updated my answer to hopefully make it more clear. You *must* grab the first capture group to get the result you want. – Sumner Evans Jul 27 '17 at 19:21
  • @SumnerEvans damn, it captures only first letter, not even word :c – user1898714 Jul 27 '17 at 19:38
  • @user1898714, did you copy-paste *directly* from my answer? When I run it using the "Run code snippet" button here on StackOverflow, it works. – Sumner Evans Jul 27 '17 at 20:13
0

If you know that underscore will always prefix the c you could do this: /:_?(.*)/g

The _? matches 0 or 1 underscores, so you will only get c as a result.

console.log('a b:_c'.match(/:_?(.*)/)[1])
88jayto
  • 659
  • 1
  • 5
  • 20
0

Do you want to get the string "c" or the last character?

var a = "a:_c a b:_c";

/c/g.exec(a)

/.$/g.exec(a)

Edit to match comment:

Use the following to remove everything before and including ":_"

a.replace(/.*:_*/, '')

For space:

a.replace(/.*:\s*/, '')
ChrisDunne
  • 15
  • 5