shiv shiv - 11 months ago 69
Javascript Question

Regex append characters to a substring

My string comes in two flavours-

var a = /aid/f82eb514073124cd10d468b74eee5663?sg=1#/propertyinfo


var a = /aid/f82eb514073124cd10d468b74eee5663#/propertyinfo

I want to append the content that comes after aid/ and before ? or # with "-test". In either of the above scenarios the result would be f82eb514073124cd10d468b74eee5663-test


a = /aid/f82eb514073124cd10d468b74eee5663-test#/propertyinfo


a = = /aid/f82eb514073124cd10d468b74eee5663-test?sg=1#/propertyinfo

Answer Source

Seems like you're looking for something like this.

Regular expression /\/aid\/[0-9A-F]*/i and replacement expression $0-test.

JavaScript is a little bit different than just plain regular expression antics, so here you go;

var a = "/aid/f82eb514073124cd10d468b74eee5663?sg=1#/propertyinfo";
alert(a.replace(/(\/aid\/[0-9A-F]*)/i, "$1-test"));