0

I want to much Redmine issue numbers in Version Control commit messages, which are numbers preceded by a # sign, eg #123, #456 etc, but I only want to match them if they are surrounded by a punctuation character set, or are the beginning or end of the line. eg '#234aa, #567' should match only #567. ', - ;#456,,' should match #456 because ',-; ' are all in the punctuation character set. I have tried an example expression

function myFunction()
{
    var str="erwet,#3456 #623 #345 fdsfsd"; 
    var n=str.match(/\#[\d+]+[\s]/g);
    document.getElementById("demo").innerHTML=n;
}

I also want to match them into an array or a list, but the demo I am trying matches them into a single string.

vfclists
  • 19,193
  • 21
  • 73
  • 92
  • clarifying so do you basically want to ignore punctuations that are deemed in this punctuation character set? I got kind of confused by your example. Also maybe you can tell us what the str you defined in your function "erwet,#3456..." should match. – aug Dec 08 '12 at 21:40
  • 1
    Maybe you're looking for `\b`, as in `"#2aa #3; #4".match(/#\d+\b/g);`. – pimvdb Dec 08 '12 at 21:41
  • 1
    Based on [your previous question](http://stackoverflow.com/q/13775527/331508), this is not what you need to ask. You want to ***split*** those refs out of a string, so that you can wrap them without losing the surrounding text. ... **When asking questions, state your true goal, and link to your previous questions that are related.** This will help people solve the true issue, even if the current question is otherwise unclear. – Brock Adams Dec 08 '12 at 22:29
  • @aug The 'erewet,' is part of the string which contains the issue numbers which need to be matched. I added it as an example of the text surrounding the issue numbers which need to be matched. 'Erewet,' should not be matched, only the issue numbers like #345, #23, #987 etc. My test regex is unable to exclude some of the surrounding characters and I am looking for one that does. – vfclists Dec 08 '12 at 22:46
  • @BrockAdams I decided not to express exactly what I wanted because the answers would be limited to what the original goal and wouldn't teach me more about regexes. My intention is to get the proper regex and use it to split the URL descrribed in http://stackoverflow.com/q/13775527/331508 – vfclists Dec 08 '12 at 23:39

1 Answers1

1

Okay, so I think I have a regex that does the job, the fact that it's matching punctuation made a couple of sentences in your example a little confusing but here goes:

var re = /(?:^|['".,;:\s])(#\d+)(?:['".,;:\s]|$)/;​

Which can be broken down as:

(?:^|['".,;\s]) //matches either the beginning of the line, or punctuation
(#\d+ )         //matches the issue number
(?:['".,;:\s]|$)//matches either punctuation, whitespace, or the end of the line

So we get:

re.test('#234aa, #567') //true
re.exec('#234aa, #567') //["#567", "#567", "", index: 8, input: "#234aa, #567"] 
re.test("', - ;#456,,'")//true
re.exec("', - ;#456,,'")//[";#456,", "#456", index: 5, input: "', - ;#456,,'"]   

I'm not so sure about the \s in the last bit, because that's neither punctuation nor end of line, but you had it in your base code so I assume it's something you want.

kieran
  • 1,537
  • 10
  • 10
  • Does your answer match the punctuation? Only the #xxx numbers are to be matched. I am also getting quoting errors when I try the re expression. Aren't the single or double quotes supposed to be escaped or are they okay in Javascript. – vfclists Dec 08 '12 at 23:37
  • Hi sorry I've been afk for the last couple hours. This code is javascript, I tested it by pasting it straight into chrome's console, and it seemed to work fine. You don't need to escape ' or " when in a reg exp literal (the two forward slashes). It won't match against the punctuation because those groups have the `?:` special character. If you want to include the two punctuation matches remove the `?:` from the bracketed expression. hope this helps – kieran Dec 09 '12 at 01:00
  • I think I wasn't clear about the question because of not knowing the right terminology. After testing the regex in pimvdb comment it is seems to do what I want. The \b option seems to match the issue numbers in the right manner – vfclists Dec 09 '12 at 12:47