Javascript get only matched text in regex

I have string like below


I want to convert the string to 13-FEB-31200-ce

so I tried below code


But its returning whole string

Two capture groups is probably the way to go. Now you have two options to use it. One is match which requires you to put the two pieces together

var str = 'BANKNIFTY-13-FEB-2020-31200-ce'
var match = str.match(/[^-]+-(\d{2}-[A-Z]{3}-)\d{4}-(.*)/)

// just reference the two groups

// or you can remove the match and join the remaining

String.prototype.match(), If you need to know if a string matches a regular expression RegExp , use RegExp.test() . If you only want the first match found, you might want to use If you want to obtain capture groups and the global flag is set, you need� Note: If the regular expression does not include the g modifier (to perform a global search), the match() method will return only the first match in the string. This method returns null if no match is found.

Regex doesn't seem to be the most appropriate tool here. Why not use simple .split?

let str = 'BANKNIFTY-13-FEB-2020-31200-ce';
let splits = str.split('-');
let out = [splits[1], splits[2], splits[4], splits[5]].join('-');

Methods of RegExp and String, The method str.match(regexp) finds matches for regexp in the string str . It has 3 If there are no matches, we don't get an empty array, but null . It's easy to But we can split by a regular expression, the same way: alert('12, 34 When the first argument of replace is a string, it only replaces the first match. World's simplest browser-based utility for extracting regex matches from text. Load your text in the input form on the left, enter the regex below and you'll instantly get text that matches the given regex in the output area. Powerful, free, and fast. Load text – get all regexp matches. Created by developers from team Browserling.

I would not use Regex at all if you know exact positions. Using regex is expensive and should be done differently if there is way. (

const strArr = "BANKNIFTY-13-FEB-2020-31200-ce".split("-"); // creates array
strArr.splice(0,1); // remove first item
strArr.splice(2,1); // remove 2020
const finalStr = strArr.join("-");

Capturing groups, It allows to get a part of the match as a separate item in the result array. Let's make something more complex – a regular expression to search for a website Just like match , it looks for matches, but there are 3 differences:. The method regexp.exec (str) method returns a match for regexp in the string str. Unlike previous methods, it’s called on a regexp, not on a string. It behaves differently depending on whether the regexp has flag g. If there’s no g, then regexp.exec (str) returns the first match exactly as str.match (regexp).

If the pattern doesn't need to be too specific. Then just keep it simple and only capture what's needed. Then glue the captured groups together.

let str = 'BANKNIFTY-13-FEB-2020-31200-ce';

let m = str.match(/^\w+-(\d{1,2}-[A-Z]{3})-\d+-(.*)$/)
let result = m ? m[1]+'-'+m[2] : undefined;


JavaScript String match() Method, Read more about regular expressions in our RegExp Tutorial and our RegExp a global search), the match() method will return only the first match in the string. Return Value: An Array, containing the matches, one item for each match,� In JavaScript, use it like. var rx = /\ ( ( [^ ()]*)\)/g; Pattern details. \ ( - a ( char. ( [^ ()]*) - Capturing group 1: a negated character class matching any 0 or more chars other than ( and ) \) - a ) char. To get the whole match, grab Group 0 value, if you need the text inside parentheses, grab Group 1 value.

You can have something like this by capturing groups with regex:

const regex = /(\d{2}\-\w{3})(\-\d{4})(\-\d{5}\-\w{2})/

const text = "BANKNIFTY-13-FEB-2020-31200-ce"

const [, a, b, c] = text.match(regex);


New JavaScript Features That Will Change How You Write Regex , Regular expressions have been part of the JavaScript language For example, the regex /Item(?= 10)/ matches Item only when it is Furthermore, you can extract the matched value by parentheses for further processing. Parentheses are numbered from left to right. The search engine memorizes the content matched by each of them and allows to get it in the result. The method str.match(regexp), if regexp has no flag g, looks for the first match and returns it as an array: At index 0: the full match. At index 1: the contents of the first parentheses.

Regular Expressions in JavaScript, If there is only one backslash, it would be interpreted by JavaScript's string parser as an escaping Pattern Matching with Regular Expression Further, you can add the global flag g to a regular expression to find all matches in a string:� Backreference \1 will contain the item to be match. If your regex flavor supports lookaround, use (?<=\[)[^]]+(?=\]) This will only match the item inside brackets.

Regular Expressions :: Eloquent JavaScript, Something with a star after it never prevents a pattern from matching—it'll just match zero instances if it can't find any suitable text to match. A question mark makes� Parentheses group the regex between them. They capture the text matched by the regex inside them into a numbered group that can be reused with a numbered backreference. They allow you to apply regex operators to the entire grouped regex. (abc) {3} matches abcabcabc. First group matches abc. YES: YES: YES: YES: YES: YES: YES: YES: YES: YES: YES

preg_match_all - Manual, Searches subject for all matches to the regular expression given in pattern and Essentially I just preg_match() unique nodes to find the values I am looking for,� Solution: Use the Java Pattern and Matcher classes, supply a regular expression (regex) to the Pattern class, use the find method of the Matcher class to see if there is a match, then use the group method to extract the actual group of characters from the String that matches your regular expression.

  • you can see , I even removed 2020
  • Well you do not need to use capture groups for things you do not care about. And get rid of the /g flag.
  • The OP also needs to remove the "2020".
  • Oh! Really, no year huh. Okay. Thanks for the remark.
  • -2020 should not be in it