user7633892
user7633892

Reputation:

Remove part of string between 2 different characters

This is my string = data-dateformat="dd-MMM-YYYY" class="info th-header-bc-ascolor">22-02-2017

Please note that dd-MMM-YYYY can be any dateformat.

What I want is to remove every thing between data-dateformat="dd-MMM-YYYY" and >

This is my best attempt, but i know it don't work.

mystring.substring(mystring.indexOf('data-dateformat="*"'), htmlcontent.indexOf('>'));

How can i solve this?

Upvotes: 6

Views: 125

Answers (1)

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

to remove every thing between data-dateformat="dd-MMM-YYYY" and >

You may try the following approach with String.prototype.replace() function and specific regex pattern:

var str = 'data-dateformat="dd-MMM-YYYY" class="info th-header-bc-ascolor">22-02-2017',
    new_str = str.replace(/(data-dateformat="[^"]+")[^>]+>/, '$1>');

console.log(new_str);

[^"]+ - will match any character except ", i.e. data-dateformat attribute value(between double quotes)

[^>]+ - will match any character except >

$1 - points to the first captured group which is (data-dateformat="[^"]+")

Upvotes: 4

Related Questions