Last active
February 18, 2024 16:35
-
-
Save projetnumero9/4e8c09f7c129af0716a725d6c50322c8 to your computer and use it in GitHub Desktop.
Search and replace a string for one or many concepts attribute (litteral string or REGEX) #jarchi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Attribute - replace string | |
// | |
// (c) 2019 David GERARD | |
// | |
// Search and replace a string for one or many concepts attributes (name, documentation) | |
// | |
// Roadmap: | |
// - when using Regex, implement global replacement if multiple occurences | |
// - when the replacementString is a Regex, some corrections and improvment still needed | |
// | |
// - Clear the console for any new message which may occur | |
console.show(); | |
console.clear(); | |
// - Prompt for the concept attribute to scan | |
var attributeName = window.prompt("Which attribute is to be scanned for string replacement:","name"); | |
// - Prompt for the string to search | |
var searchedString = window.prompt("Which is the string to search:"); | |
var regexYorN = window.prompt("Is the searchedString a RegEx (Y or N):","N"); | |
// - Prompt for the string type entered | |
switch(regexYorN) { | |
case ("Y"): case ("y"): | |
var searchedString = new RegExp(searchedString); | |
break; | |
case ("N"): case ("n"): | |
var message="String it is, then." | |
window.alert(message); | |
console.log(message); | |
break; | |
default: | |
var message="Y(es) or N(o), please, upper- or lowercase, as you wish!" | |
window.alert(message); | |
console.log(message); | |
exit(); | |
} | |
// - Prompt for the replacement string | |
var replacementString = window.prompt("Which is the replacement string:"); | |
var regexYorN = window.prompt("Is the replacementString a RegEx (Y or N):","N"); | |
// - Prompt for the string type entered | |
switch(regexYorN) { | |
case ("Y"): case ("y"): | |
var replacementString = new RegExp(replacementString); | |
break; | |
case ("N"): case ("n"): | |
var message="String it is, then." | |
window.alert(message); | |
console.log(message); | |
break; | |
default: | |
var message="Y(es) or N(o), please, upper- or lowercase, as you wish!" | |
window.alert(message); | |
console.log(message); | |
exit(); | |
} | |
// - for each selected element, | |
selection.each(function(selectedElement) { | |
// - Get the content of the desired concept attribute | |
// Source: | |
// https://stackoverflow.com/questions/2241875/how-to-create-an-object-property-from-a-variable-value-in-javascript | |
// http://www.ecma-international.org/ecma-262/5.1/#sec-11.2.1 | |
var text = selectedElement[attributeName]; | |
// - Replace searchedString with replacementString if desired concept property value not empty and searchedString found | |
if (text) { | |
text = text.replace(searchedString, replacementString); | |
} | |
// - Update the desired concept attribute content if a replacement has been operated | |
if (selectedElement[attributeName] != text) { | |
console.log("Concept updated: ",text); | |
selectedElement[attributeName] = text; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment