2

I have string like below

BANKNIFTY-13-FEB-2020-31200-ce

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

so I tried below code

str.match(/(.*)-(?:.*)-(?:.*)-(.*)-(?:.*)-(?:.*)/g)

But its returning whole string

2
  • you can see , I even removed 2020 Commented Feb 6, 2020 at 18:11
  • Well you do not need to use capture groups for things you do not care about. And get rid of the /g flag. Commented Feb 6, 2020 at 18:17

5 Answers 5

4

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
console.log(`${match[1]}${match[2]}`)

// or you can remove the match and join the remaining
match.shift()
console.log(match.join(''))

Or just string replace which you do the concatenation of the two capture groups in one line.

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

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

Comments

2

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('-');
console.log(out);

If you really want to use regexp,

let str = 'BANKNIFTY-13-FEB-2020-31200-ce';
let splits = str.match(/[^-]+/g);
let out = [splits[1], splits[2], splits[4], splits[5]].join('-');
console.log(out);

Comments

1

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. (https://blog.codinghorror.com/regular-expressions-now-you-have-two-problems/)

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("-");

Comments

1

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;

console.log(result);

In this regex, ^ is the start of the string and $ the end of the string.

2 Comments

The OP also needs to remove the "2020".
Oh! Really, no year huh. Okay. Thanks for the remark.
0

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

enter image description here

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);

console.log(`${a}${c}`)

1 Comment

-2020 should not be in it

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.