1

I have string [FBWS-1] comes first than [FBWS-2]

In this string, I want to find all occurance of [FBWS-NUMBER]

I tried this :

var term = "[FBWS-1] comes first than [FBWS-2]";
    alert(/^([[A-Z]-[0-9]])$/.test(term));

I want to get all the NUMBERS where [FBWS-NUMBER] string is matched.

But no success. I m new to regular expressions.

Can anyone help me please.

2
  • @AndrewBone, i tried this as well regexper.com/… but still not working Commented Jun 27, 2018 at 8:02
  • @WiktorStribiżew closing bracket Commented Jun 27, 2018 at 8:02

4 Answers 4

3

Note that ^([[A-Z]-[0-9]])$ matches start of a string (^), a [ or an uppercase ASCII letter (with [[A-Z]), -, an ASCII digit and a ] char at the end of the string. So,basically, strings like [-2] or Z-3].

You may use

/\[[A-Z]+-[0-9]+]/g

See the regex demo.

NOTE If you need to "hardcode" FBWS (to only match values like FBWS-123 and not ABC-3456), use it instead of [A-Z]+ in the pattern, /\[FBWS-[0-9]+]/g.

Details

  • \[ - a [ char
  • [A-Z]+ - one or more (due to + quantifier) uppercase ASCII letters
  • - - a hyphen
  • [0-9]+ - one or more (due to + quantifier) ASCII digits
  • ] - a ] char.

The /g modifier used with String#match() returns all found matches.

JS demo:

var term = "[FBWS-1] comes first than [FBWS-2]";
console.log(term.match(/\[[A-Z]+-[0-9]+]/g));

Sign up to request clarification or add additional context in comments.

2 Comments

Maybe the OP only wants single digit, So maybe drop the + quantifier.
@RavinderSingh I added a note that in case you want to hardcode FBWS, you should use it instead of [A-Z]+.
0

You can use:

[\w+-\d]

var term = "[FBWS-1] comes first than [FBWS-2]";
alert(/[\w+-\d]/.test(term));

There are several reasons why your existing regex doesn't work.

  • You trying to match the beginning and ending of your string when you actually want everything in between, don't use ^$
  • Your only trying to match one alpha character [A-Z] you need to make this greedy using the +

You can shorten [A-Z] and [0-9] by using the shorthands \w and \d. The brackets are generally unnecessary.

Note your code only returns a true false value (your using test) ATM it's unclear if this is what you want. You may want to use match with a global modifier (//g) instead of test to get a collection.

2 Comments

] is not a special regex metacharacter outside of a character class, so no need escaping it. You only need to escape it when it is inside a character class.
It is not a big problem here, but in case you have to use the pattern in an ES6 compliant regex with u modifier, it might be a problem then.
0

Here is an example using string.match(reg) to get all matches strings:

var term = "[FBWS-1] comes first than [FBWS-2]";
var reg1 = /\[[A-Z]+-[0-9]\]/g;
var reg2 = /\[FBWS-[0-9]\]/g;
var arr1 = term.match(reg1);
var arr2 = term.match(reg2)

console.log(arr1);
console.log(arr2);

Comments

0

Your regular expression /^([[A-Z]-[0-9]])$/ is wrong.

Give this regex a try, /\[FBWS-\d\]/g

remove the g if you only want to find 1 match, as g will find all similar matches

Edit: Someone mentioned that you want ["any combination"-"number"], hence if that's what you're looking for then this should work /\[[A-Z]+-\d\]/

6 Comments

IMHO, the [A-Z], the original pattern implies that there can be ABCD, too, not only FBWS
I'm presuming the OP wants to match other alpha numeric patterns not just that one
IMHO, this is the best answer, the question says "I want to find all occurrences of [FBWS-NUMBER]" which means he wants to match other numeric patterns
But this doesn't match other alphanumeric patterns @HyyanAboFakher?
Hmm, guess the OP needs to be more specific on what he wants as the regex can be too broad or too narrow. Though if the OP wants ["any combination"-"number"], then this should work /\[[A-Z]+-\d\]/
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.