1

i need to replace the random string after underscore here is my code

var map_list_tmpl_pre = '<div>some odd</div><ul><li class="single_map_list" onClick="showlist(\'parent_id_3aXw\')"></li>
    <li class="single_map_list" onClick="showlist(\'parent_id_3aXw\')"></li>
    </ul>';

$(map_list_tmpl_pre).filter('.single_map_list').each(function(index, currentLink) {
    alert(currentLink.outerHTML.replace(/(parent_id_)[0-9][a-z][A-Z]/, 'parent_id_newID'));
});

1 Answer 1

2

Change [0-9][a-z][A-Z] to [0-9a-zA-Z]+

[0-9][a-z][A-Z] would mean one digit followed by one lowercase letter followed by one upper case letter, e.g. this would match:

5aA

but this wouldn't:

5Aa

So using your current example, it would match parent_id_3aX in the parent_id_3aXw string, and replace it so it ended up being parent_id_newIDw

Using [0-9a-zA-Z]+ means that one or more of the pattern would match (signified by the +. And it could be any character in the 0-9 and a-z and A-Z ranges, order wouldn't matter.

Alternatively, instead of having to specify both lowercase and uppercase ranges, you could simply add the i flag to make it case-insensitive:

replace(/(parent_id_)[0-9A-Z]+/i

Also you shouldn't need to wrap parent_id_ in parentheses, unless you're wanting to reference it later with a back-reference. This should also work:

replace(/parent_id_[0-9A-Z]+/i

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

1 Comment

Great Working like charm :) Thanks Duncan

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.