Javascript replace text within <title> with <span>title</span> - javascript

Hi i want to replace my javascript string solution like this.
input:- this is <title> and <heading>
output:- this is <span>title</span> and <span>heading</span>
can anyone help me on this.
Thanks in advance.

use regex to replace
input.replace(/</g, '<span_').replace(/>/g,'</span>').replace(/_/g,'>');

Try something like this:
var input = "this is <title> and <heading>";
var output = input.replace(/<([^>]+)>/g, "<span>$1</span>");
console.log(output);
That is, match an opening <, followed by one or more non-> characters captured as a submatch so that you can reference it as $1 in the replacement string, followed by a closing >.

Another alternative
var output = input.split("<").join("#").split(">").join("</span>").split("#").join("<span>");
console.log(output);

Use
input.replace(/<[a-zA-Z]*>/g, '<span>$&</span>');
Explanation:
.replace encloses all tags in ''. '$& is replaced with the tag. See this for more info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
Example:
var ta = document.getElementById('ta'), output = document.getElementById('output');
ta.value = 'I have <title> and <head> (this is just example text, you can replace).';
function replaceIt() {
output.innerText = ta.value.replace(/<[a-zA-Z]*>/g, '<span>$&</span>');
// Okay, technically Node#innerText is non-standard, so use an HTML escape function on production sites.
// For demo purposes, I just chose innerText for ease of use
}
<textarea id="ta"></textarea>
<button onclick="replaceIt()">Enclose tags in &ltspan&gt s.</button>
<div>Output:</div>
<div id="output"></div>

Related

JavaScript function to identify URL and make them clickable

I have a requirement , my client send me a string. for the links he is sending link title in squre brackets and link with bracket. like below,
[Google](https://www.google.com/)
I need get that value and make it clickable Google . adding like below and replace that to the original text.
' + url + ''
can anyone suggest better way of doing this with JavaScript regex.
Looks like Markdown formatting, so you could use a markdown library like Marked to parse and render it:
const s = '[Google](https://www.google.com/) ';
document.getElementById('content').innerHTML = marked(s);
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
<div id="content"></div>
Can be done with String replace function.
Regex: /\[(.*)\]\s*\((.*)\)/g
Replacer: $1
const str = `Lorem ipsum. [Google](https://www.google.com/). Sample text.`
const output = replaceWithLinks(str);
console.log(output);
function replaceWithLinks(str) {
return str.replace(/\[(.*)\]\s*\((.*)\)/g, '$1')
}
In HTML file:
<h1 id="header"></h1>
In Js File:
const myString = "[Google](https://www.google.com/)";
const show = myString.match(/\[(.*?)\]/); // it return two things. The first is with bracket and the second one is without bracket you have to use without bracket.
const url = myString.match(/\((.*?)\)/);
document.getElementById("header").innerHTML = `${show[1]}`;
You have to use regular expression. To get information about regular expression read MDN.
Get the first index value and show it to the UI.
Regexp is very handy for this purpose. just copy below code to F12 console for a preview
"text before [Google](https://www.google.com/) and after".replace(/\[(.*?)\]\((.*?)\)/gm, '$1')
ps: the code copy from a simple markdown parser

getElementsByTagName exclude script tag

As the title suggests I want to exclude the script tag.
Cause while using regex (at least I think that's the right name :P)
I get to a point where something
var wdc = /something/g;
is included inside the
var foundwdc = words.match(wdc).length;
So when I alert foundwdc it gives 3 "somethings" instead of the desired two inside the body
var words = document.getElementsByTagName('body')[0].innerHTML;
Hope this is clear enough :D and hope the title is right :P
Use replace() to remove the script tag from string
var words = document.getElementsByTagName('body')[0].innerHTML.replace(/<script[^>]*>[\s\S]*?<\/script>/gi, '');
console.log(words);
hi hello
<script>
</script>
Regex explanation here.

JavaScript Strip Vowels

I am trying to strip vowels in a string. I know I should be using str.replace but I am baffled on how to get it into a span.
This is more of less what i am looking to do:
Write a JavaScript function that takes in a string s and returns the string which is equivalent to s but with all ASCII vowels removed. EX: (“Hello World”) returns: "Hll wrld"
Please help!
.replace(/[aeiou]/ig,'') is all you need.
To replace vowels you can use a simple regular expression:
function removeVowels(str) {
return str.replace(/[aeiou]/gi, '');
}
As for the part about getting it into a span, not 100% sure what you mean, but maybe something like this:
<span id="mySpan">Hello World!</span>
<script>
var span = document.getElementById('mySpan');
span.innerHTML = removeVowels(span.innerHTML);
</script>
string.replaceAll("[aeiou]\\B", "")

javascript to replace a text within [] with hyperlink

Am in a process of writing a javascript to replace a text within [] to a html link. But am stuck at generating a regular expression to match any string that is in [] and then replace it with a hyperlink.
Below is my code snippet that i have tried:
<html>
<head>
</head>
<body id="body">
Hello World [1234]<br>
[322]<br>
Hello
</body>
</html>
<script type="text/javascript">
var bodyText=document.getElementById('body').innerHTML;
var pattern = "\[(.*?)\]";
var replaceText = "Pradeep";
document.getElementById('body').innerHTML = bodyText.replace(pattern/gi, replaceText);
</script>
Can anyone please suggest me a best way to do it
Thanks,
If you have a string representing a regexp, you have to call new RegExp(str) to build a regexp from it, but you can just make a regexp literal here. Also, the i flag is not necessary since nothing in your regexp refers to letters. And, you don't use the capturing groups so you can eliminate them as well. Lastly, you need to escape the quotes in the string because the interpreter now thinks your strings ends after href=:
var bodyText = document.getElementById('body').innerHTML;
var pattern = /\[.*?\]/g;
var replaceText = "Pradeep";
document.getElementById('body').innerHTML = bodyText.replace(pattern, replaceText);
I tried \[[^\]]+\] as regexp and got the right output:
var s = 'Hello World [1234]<br>[322]<br>';
s = s.replace(/\[[^\]]+\]/ig, 'Pradeep');
// output
// Hello World Pradeep<br>Pradeep<br>

How do I alter a string between specific points in JavaScript?

Say I want to change the text in between '[code][/code]' and make the text monospace (CSS) Just like Quora does, but only simpler. How would I go about doing this?
For example:
String - "Here is some code: [code]Hello[/code]"
Here's a simple way using a RegExp to replace the [code] tags with <pre> tags:
yourString.replace(/\[code\](.*)\[\/code\]/ig, '<pre>$1</pre>');
See example →
You could use regex, something like this:
result = subject.replace(/\[code\](.*?)\[\\/code\]/g, "[code]$1 World[/code]");
The above will replace "Hello" with "Hello World"
Try using a regex:
str = str.replace(/(\[code\]).*?(\[\/code\])/, '$1Good Bye$2');

Categories

Resources