This question already has answers here:
How to use split?
(4 answers)
Closed 6 years ago.
I have a blog and blog titles are like this;
"Hey There: Part 1"
So, is there any chance to break the line after ":", because I want Part 1 to start a new line. So it should be like this:
"Hey there:
Part 1"
But there are so many titles, so I want to do this with a javascript code to all of those titles. Is this possible?
You can use split() of JavaScript
var text = "Hey There : Part 1";
var newText = text.split(":").join('\n');
alert(newText);
Using jQuery:
var blogTitle = $('theTitleElement')
$(blogTitle).each(function(){
$(this).html(this.textContent.split(':').join('<br>'))
});
%0D%0A are the characters for a carriage return and line break if you're URL encoding.
%0D is a carriage return character %0A is a line break character.
If you want to line break in your JavaScript code then you can use \n to escape a line break.
var sOrig = "Hey There: Part 1";
sOrig.split(':').join('\n');
you can replace the \n with html etc if needed or just do a replace()
Related
This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 2 years ago.
So I'm trying to make an a sort of auto correct system in my website, and I want to detect a word and if the word is in the image source, then replace it. In my code I am trying to replace the word "Dunk" with SB Dunk. In the source it says Dunk twice, but it only replaces the first time it uses "Dunk", and then keeps making errors and keeps adding more "SB's" to the "Dunk". Heres my code.
//https://stockx-360.imgix.net//Nike-Dunk-Low/Images/Nike-Dunk-Low/Lv2/img01.jpg?auto=format,compress&w=559&q=90&dpr=2&updated_at=1580325806`.replace('%20', '-')
shoesimg.addEventListener('error', function(){
if(shoesimg.src.includes('Dunk')){
const newshoesimg = shoesimg.src.replace(/Dunk/, 'SB Dunk');
shoesimg.src = newshoesimg;
}
You can use replaceAll. Following is the example,
let replacedStr = "1 abc 2 abc 3".replaceAll("abc", "xyz");
// replacedStr is "1 xyz 2 xyz 3"
Another way would be to find the indexes of the word dunk and then using for loop replace one-by-one.
Just add 'g' at the end of regular expression:
shoesimg.addEventListener('error', function(){
if(shoesimg.src.includes('Dunk')){
const newshoesimg = shoesimg.src.replace(/Dunk/g, 'SB Dunk');
shoesimg.src = newshoesimg;
}
https://dmitripavlutin.com/replace-all-string-occurrences-javascript/
Try String.replaceAll():
const newshoesimg = shoesimg.src.replaceAll('Dunk', 'SB Dunk');
This question already has answers here:
Creating multiline strings in JavaScript
(43 answers)
Closed 7 years ago.
EDIT OK, I've realised there is already an answer answering this question with NO: Creating multiline strings in JavaScript
Thanks for all escaping/concatenating answers, but it is not what I needed.
END EDIT
In Python it is possible to define string variables having many lines by the notation
"""
many
many lines
"""
Is there something like this in JavaScript?
I think you could use it like this:
var str = 'many\n' +
'many lines';
Although such a thing does not exist in Javascript there is a way around it. Instead of the occasional """ """, you can add a \ at the end of each line. That in turn will create a "Multi-line string". Here is an example:
var myString = "This is \
my multi \
line string";
EDIT
I thought I should also point this out. Another way of accomplishing this is to concatenate together the strings, like so:
var myString = "This is" +
"my multi" +
"line string";
This question already has answers here:
Are double and single quotes interchangeable in JavaScript?
(23 answers)
Closed 8 years ago.
I am building a website that teaches people how to code a website. I am trying to add a feature where they code the exercise into a text box and I then compare that to a string to see if they got it right or not yet. I am running into an issue though when there is quotes inside the strings answer because that then ends the string thus cutting off some of the answer. How can I get around this?
All feedback is greatly appreciated!
Here is an example of a strings answer that screws it up:
var answer = "var greeting="Hello World!"; ";
The second pair of quotes end the string's declaration early. Is there a way to include all of it including the second pair of quotes in the declaration?
You can:
Escape the quotes with \:
var answer = "var greeting=\"Hello World!\"; ";
var answer = 'var greeting=\'Hello World!\'; ';
Use different quotes:
var answer = "var greeting='Hello World!'; ";
var answer = 'var greeting="Hello World!"; ';
This question already has answers here:
How to put variable in regular expression match?
(3 answers)
Closed 8 years ago.
Sorry If I ask some silly question, but I don't know what to do with it during 2 days.And I need your help.
That's what I want:
var str = "Hello World by Wor";
if(str.match(/\bWor\b/)){
alert('He is here');
}
And it's work, but if I use a Variable:
var str = "Hello World by Wor";
var sear = "Wor";
if(str.match(/\bsear\b/)){
alert('He is here');
}
It doesn't work like example before.
Important: I need to use tags "\b" for make a border for search string.
var str = "Hello World by Wor";
var sear = /Wor/g;
if(str.match(sear).length){
alert("reached")
}
FIDDLE DEMO
NOTE: The g flag is must to get all matches instead of just the first one.
EXPLANATION
This question already has answers here:
How to split a long regular expression into multiple lines in JavaScript?
(11 answers)
Closed 8 years ago.
This one seems like it has a very simple answer, yet I can't find it anywhere. I have a regular expression that is quite large, how do I put in some line breaks in the expression itself so I don't have to keep scrolling horizontally through the code to see it all?
I don't normally use word-wrap, and the IDE I'm using doesn't even offer it anyway.
A line break in a string would normally be a \ at the end of the line :
var mystring "my string \
is on more \
than one line";
var re = new RegExp(mystring, "gim");
You could use RegExp and .join() to convert and concat a string.
var myRegExp = RegExp(['/^([a-zA-Z0-9_.-])+'
,'#([a-zA-Z0-9_.-])+'
,'\.([a-zA-Z])+([a-zA-Z])+/'].join(''));
The answer has been linked to here as well.
How to split a long regular expression into multiple lines in JavaScript?