I want to replace all characters in a specific class with another character using JavaScript
JavaScript is just a beginner and may be a basic question ...
<p>apple and orange and banana</p>
<p>apple and orange and banana</p>
This is a script (Tempermonky) only
<p>Apple, Orange and Banana</p>
<p>apple and orange and banana</p>
I want to do that.
I just started JS and I don't know anything.
I don't even know what order (what is the official name?)
-
Answer # 1
-
Answer # 2
It's not limited to JavaScript, but it's just a string from the programming language
I think I have to have a "conversion dictionary" somewhere (for example, by an array) and replace the corresponding information.There is basically no concept of "word" except for the function of programming language.
apple is just a collection of the letters [a] [p] [p] [l] [e]
An apple is just a collection of the letters [ri] [n] [go].You can also define variables and functions yourself, but that's just a collection of strings, so
Even if a combination is not found in an existing word, the programming language will only interpret it as "defined by that name".As presented now
apple and orange and banana
It may be easy to understand if it is separated by words and phrases (because it can be converted into words separated by spaces)
What would you do if you were connected likeappleandorangeandbanana
?
Or
apple and orange and bana- na
What to do if there is a line break in the middle of a word like
Or
[and] also changes to various meanings depending on the context (like "~~~ and")or
I think there are a lot ofrequirements that must be met before thinking about implementation.Make sure that everything is properly packed, and then enter the string operation instead of the DOM operation.
I think that the replacement process, especially the replacement process with regular expressions, will be necessary this time. -
Answer # 3
I think that the replacement will not work if there are multiple matches, but you can replace all with the g flag.
const substr = ['and', 'apple', 'orange', 'banana']; const newstr = ['and', 'apple', 'orange', 'banana']; document.querySelectorAll ('. sample1'). forEach ((p, index) =>{ Array.from (p.childNodes) .filter (node =>node.nodeType === Node.TEXT_NODE) .forEach (text =>{ let value = text.nodeValue; for (let i = 0;i
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/RegExp -
Answer # 4
Tempermonky is for the time being
If you just want to change the value of the sample1 class to a new oneapple and orange and banana
apple and orange and banana
However, because it is a class process, the sample1 class may come in multiple places
Multiple versions
If special processing is required, such as changing the letter A to the letter B
A mechanism that supports it is necessary.
(The example sample simply changes the text to a new one without any comparison)apple and orange and banana
apple and orange and banana
apple and orange and banana
apple and orange and banana
apple and orange and banana
apple and orange and banana
Related articles
- javascript - i want to replace a class component with a function component
- javascript - when an element with a specific class name is clicked, give an arbitrary class
- i want to replace characters with javascript and output
- i'm making a typing game with javascript, but i don't see the following characters after typing
- ruby on rails - how to test that a specific element has a class name using rspec/capybara
- javascript - gas: i want to get a range of cells in a spreadsheet and replace the values according to the conditions
- javascript - i want the characters contained in a variable to have the meaning of an array
- i want to open a csv file with specific characters in the file name in python
- javascript - i want to display it softly and switch characters
- javascript - i want to make an implementation that changes characters in roulette format
- javascript - jquery i want to select a specific element
- javascript - i want to make it a specification that characters appear randomly just by pushing i'm getting an error
- javascript - i want to apply a method only to a specific element for the element acquired by queryselecterall
- javascript - i want to hide only one specific column of a dynamically created table
- javascript - if i replace the line sample code with typescript, i get an error
- javascript - i want to use a value initialized in one class in another unrelated class with no arguments
- javascript - i want to automatically break characters according to the width of the image with css || js
- javascript - how to get the closest distance from a specific part among multiple elements with jquery
- javascript - when posting using actioncable, the characters "undefined" will be displayed on the other party's screen
- javascript - when i try to insert an array string as a class, it becomes undefined
- javascript : How to get the size of the browser window using js or python and transfer this size to the css file?
- javascript : Adjusting window.open () window height for different screens
- javascript : Distance between menu items
- javascript : How to display part of an image on a website
- javascript : Two-column dropdown menu
- javascript : How do I make a regular div block work like input range?
- javascript : Encoding consisting of /letters and numbers
- javascript : Display the 1st column of the dropdown menu [duplicate]
- javascript : How to change the style file in index when clicking on the slider?
- javascript : HTML + CSS + JS. Working with canvas does not save image
If you search for "replace", you will get various things.
String.prototype.replace ()