How to replace a html tag in js?
If you want to stick with your simple string manipulation, you need to use regular expressions and correct the replacements in your replace
calls:
text = text.replace(/<font[^>]*>/g,'<p>').replace(/<\/font>/g,'</p>');
Find/Replace regex to remove html tags
This works for me Notepad++ 5.8.6 (UNICODE)
search : <option value="\d+">(.*?)</option>
replace : $1
Be sure to select "Regular expression" and ". matches newline"
Replace words in the body text
To replace a string in your HTML with another use the replace method on innerHTML:
document.body.innerHTML = document.body.innerHTML.replace('hello', 'hi');
Note that this will replace the first instance of hello
throughout the body, including any instances in your HTML code (e.g. class names etc..), so use with caution - for better results, try restricting the scope of your replacement by targeting your code using document.getElementById or similar.
To replace all instances of the target string, use a simple regular expression with the g
lobal flag:
document.body.innerHTML = document.body.innerHTML.replace(/hello/g, 'hi');
Replace HTML tag with string that contains tag attribute
As you are in JavaScript, you have a DOM parser at your fingertips. Use it!
const input = `the quick <input type="button" disabled="" value="brown.fox" /> jumps over the <input type="button" disabled="" value="lazy.dog" />`;
const container = document.createElement('div');
container.innerHTML = input;
const buttons = container.querySelectorAll("input[type=button]");
buttons.forEach(button=>{
button.replaceWith("${"+button.value+"}");
});
const output = container.innerHTML;
How to replace all html tags of one kind with another
You can make use gsub
with block
string = "<p><i>Random stuff here...</i></p>"
string.gsub(/(<\/?)i(>)/) { "#{$1}em#{$2}" }
#=> "<p><em>Random stuff here...</em></p>"
Explanation:
Match an i
html opening or closing tag and replace it with em
How to replace text containing string with a HTML tag?
I assume you don't literally mean all text on a page, but rather all text inside of a given element. DucFilans is right on the money with regex, I'll slightly modify his answer to provide a complete working solution:
var parserRules = [ { pattern: /bold\("(.*?)"\)/g, replacement: '<b>$1</b>' }, { pattern: /italics\("(.*?)"\)/g, replacement: '<i>$1</i>' }];
document.querySelectorAll('.parse').forEach(function(tag) { var inner = tag.innerHTML; parserRules.forEach(function(rule) { inner = inner.replace(rule.pattern, rule.replacement) }); tag.innerHTML = inner;});
<div class='parse'> <div>bold("Hello") world <p> Rose italics("pink") slow bold("!") </p> </div> <div>bold("astiago") cheeeeeeese!</div></div>
Find and replace html tags using regex
You need to do a match to focus on the text between the dollar signs and then do the replace 'text'.match(/\$(.+)\$/)[1].replace(/(<.+?>)/g, '')
You can do .match(/\$(.+?)\$/)[1]
if you have several PAIRS of dollars
replace string text with html tag using regex
I'm not sure this is what you really need. Anyway, I think it's a step closer.
I made a new regex to get '#' + words and for the text to be updated with replace it is necessary to play
text = text
Then:
import "./styles.css";
export default function App() {
let text = "#Jim, Start editing to see some magic happen!";
const regex = /\#[a-zA-Z]+/gm;
text = text.replace(regex, (match) => {
return `<span style="color: red">${match}</span>`;
});
return (
<div className="App">
<div
dangerouslySetInnerHTML={{
__html: text
}}
/>
</div>
);
}
Vscode Find & Replace a changing block of HTML code
The regex you're looking for in this case is:
<h2>((.|\n)*)<\/table>
VS Code supports regex base search & replace. Next to the search field, select the .* icon. Run the search and hit the 'replace' button, and you're done!
https://regex101.com/ is a good place to try out your regex against some sample input.
Related Topics
Share Style Across Web Components "Of The Same Type"
Export HTML Content to PDF Using Jasperreports
Show My Location on Google Maps API V3
How to Get a Div to Resize Its Height to Fit Container
How to Make Rounded Border in Ie8 with CSS
Responsive Video Iframes (Keeping Aspect Ratio) with Only CSS
How to Include Glyphicons in Bootstrap 3
How Put Percentage Width into HTML Canvas (No CSS)
Align Flex Items with Different Heights in The Same Container
Equal Height Columns with Centered Content in Flexbox
Why Does Border: 5Px Dashed Not Come Out as Dashed in Firefox
Open Local Files(File://) Using Chrome
Text-Overflow: Ellipsis in Table-Cell Without Width