shiv shiv - 2 months ago 17
Javascript Question

Regex append characters to a substring

My string comes in two flavours-

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


or

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

hence

a = /aid/f82eb514073124cd10d468b74eee5663-test#/propertyinfo


or

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

Answer

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