This question already has answers here:
How to match multiple occurrences of a substring
(3 answers)
Closed 7 years ago.
When I've studied the code given by my professor, I found a line that I do not quite understand.
The code is like this:
textclean = textclean.replace(/ your /g," ");
I think the meaning is to replace your with a space so we can delete it by split(" "); but what is the meaning of /g?
Why can't we use:
textclean = textclean.replace(your, " ");
It means replace globally, not only the 1st occurance. If you have "Foo your bar mooo your example" then without the /g only the 1st " your " would be replaced.
Related
This question already has answers here:
Delete a line of text in javascript
(8 answers)
Closed 10 months ago.
I have a variable that contains a string with lots of line breaks.
myvariable = '
1234556788,3434343434\n
1244556788,7234343834\n
1234556788,4434343434\n
1234556784,8434343434\n
1234556782,9444343434\n
';
I need some way of removing just the first one without leaving a blank first line if possible.
So that this:
1234556788,3434343434\n
1244556788,7234343834\n
1234556788,4434343434\n
1234556784,8434343434\n
1234556782,9444343434\n
turns into this:
1244556788,7234343834\n
1234556788,4434343434\n
1234556784,8434343434\n
1234556782,9444343434\n
So it would remove the first line after the \n
How can I do this?
myvariable = `1234556788,3434343434\n
1244556788,7234343834\n
1234556788,4434343434\n
1234556784,8434343434\n
1234556782,9444343434\n
`;
myvariable.substring(myvariable.indexOf('\n') + 2, myvariable.length);
This question already has answers here:
Get text between two rounded brackets
(7 answers)
Closed 2 years ago.
I have a string like Manila (Philippines) and want to replace it with only the substring Philippines. I tried using the following regex pattern, which works in Notepad++:
[^\(]+ \(([^\)]+)\)
However, I get an undefined result in JavaScript:
var x = "Manila (Philippines)";
console.log(x.replace(/[^\(]+ \(([^\)]+)\)/,$1));
You just forgot the " around your replace pattern!
console.log(x.replace(/[^\(]+ \(([^\)]+)\)/,"$1")); will work correctly!
You can use .match():
var x = "Manila (Philippines)";
var result = x.match(/\((.+)\)/).pop();
// regex for string contained in parentheses
console.log(result);
This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 5 years ago.
I'm trying to replace every word between %% with a single word. Till now I tried this:
var string = "You chose %story% at %price%";
var rep = string.replace(/\%(.*)\%/g, "test");
console.log(rep);
but the result of rep is actually
"You chose test"
while I want
"You chose test at test"
How can I achieve this? I though about implementing a recursive function but it sound pretty slow especially with multiple words
Try the snippet below, just put a lazy quantifier ? after *, so that it will not take more than one occurrence
var string = "You chose %story% at %price%";
var rep = string.replace(/%(.*?)%/g, "test");
console.log(rep);
This question already has an answer here:
JS regex replace not working
(1 answer)
Closed 5 years ago.
I'm trying to get only AN from this string, but replace function seems not working, I end up getting exactly the same string every time... any hits?
building = "\n AN ";
var temp = building.replace("\\W", "");
You need the meta sequence any non-word character \W and the global flag g for a continuing replacement.
var building = "\n AN ",
temp = building.replace(/\W/g, "");
console.log(temp);
This question already has answers here:
How do I make the first letter of a string uppercase in JavaScript?
(96 answers)
Closed 8 years ago.
Now I have this regex:
tSzoveg = tSzoveg.replace(/\b[A-Z]{2,}\b/,'');
It is almost that I want. I want to convert this: THIS IS MINE, NOT YOURS. to this: This is mine, not yours.
How can I convert it to a normal sentence?
function capitalize(string)
{
return string.toUpperCase().charAt(0) + string.toLowerCase().slice(1);
}
I would use toLowerCase() and then modify the first letter:
var str = "THIS IS MINE, NOT YOURS."
str.charAt(0).toUpperCase() + str.slice(1).toLowerCase();