Thursday 15 January 2015

regex - how to remove all text except one in javascript? -



regex - how to remove all text except one in javascript? -

im trying remove text except 1 in javascript

the thought

//retrieve "cnode_72 cnode_1 layout_1 ui-datepicker-week-end " // "ui-datepicker-week-end" not necessary nowadays var classes= jquery("someelement").attr("class"); classes = classes.replace(/(?!ui-datepicker-week-end)/,'');

the expected ouput :

ui-datepicker-week-end

javascript (as per question)

erm... might missing here, how about:

classes = "ui-datepicker-week-end";

if text isn't in original string, , don't want add together if isn't there seek this:

if(classes.indexof("ui-datepicker-week-end") == -1) classes = ""; else classes = "ui-datepicker-week-end";

jquery (as per request in comments)

if bizarre reason using jquery("...").attr("class") didn't bother mentioning that, seek this:

jquery("...").removeattr("class").addclass("ui-datepicker-week-end");

or condition:

if(jquery("...").hasclass("ui-datepicker-week-end")) jquery("...").removeattr("class").addclass("ui-datepicker-week-end"); else jquery("...").removeattr("class");

here working example

javascript regex

No comments:

Post a Comment