Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript to edit all script tags 1

Status
Not open for further replies.

markronz

IS-IT--Management
Mar 20, 2007
93
US
Hello all-
I need to replace all <tags> and make them look like <ws:tags> in a provided string. So for example, if this string was provided:

<recipientScheduleDetail>
<scheduleName>Mark Test</scheduleName>
<division>/IT//division>
<shifts>
<shiftName>Test</shiftName>
<importedShift>false</importedShift>
</shifts>
</recipientScheduleDetail>


And I'd like the desired output to be this:
<ws:recipientScheduleDetail>
<ws:scheduleName>Mark Test</ws:scheduleName>
<ws:division>/IT//ws:division>
<ws:shifts>
<ws:shiftName>Test</ws:shiftName>
<ws:importedShift>false</ws:importedShift>
</ws:shifts>
</ws:recipientScheduleDetail>



How could I do this? I need to replace and add the "ws:" to all tags, both the opening and the closing ones. Any help would be appreciated!

Thanks!
 
these two replacements seems to work
Code:
str = `
<recipientScheduleDetail>
  <scheduleName>Mark Test</scheduleName>
  <division>IT</division>
  <shifts>
    <shiftName>Test</shiftName>
    <importedShift>false</importedShift>
  </shifts>
</recipientScheduleDetail>
`
console.log("Original string:");
console.log(str);

str = str.replaceAll(/<([^\/])/g, "<ws:$1");
str = str.replaceAll("</", "</ws:");

console.log("\nModified string:");
console.log(str);

Output:
Code:
Original string:

<recipientScheduleDetail>
  <scheduleName>Mark Test</scheduleName>
  <division>IT</division>
  <shifts>
    <shiftName>Test</shiftName>
    <importedShift>false</importedShift>
  </shifts>
</recipientScheduleDetail>

Modified string:

<ws:recipientScheduleDetail>
  <ws:scheduleName>Mark Test</ws:scheduleName>
  <ws:division>IT</ws:division>
  <ws:shifts>
    <ws:shiftName>Test</ws:shiftName>
    <ws:importedShift>false</ws:importedShift>
  </ws:shifts>
</ws:recipientScheduleDetail>
 
Hi

Just matter of personal taste, I would prefer to do it with a single replacement :
Code:
str [teal]=[/teal] str[teal].[/teal][COLOR=orange]replaceAll[/color][teal]([/teal][fuchsia]/<\/?/g[/fuchsia][teal],[/teal] [i][green]"$&ws:"[/green][/i][teal])[/teal]
Probably would need an enormous XML to notice anything, but I read recommendations to reduce the number of capturing groups used as they slow things down.


Feherke.
feherke.github.io
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top