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);
Related
This question already has answers here:
Regex to Get Phone Numbers From String
(3 answers)
Closed 10 months ago.
I have spent two days on this and I can't figure it out. Sorry to sound specific. I am trying to match phone numbers in a string and store them in an array. For example:
// An example string
let string = "30000 loaves of bread were purchased by +1777654352"
// I got this from https://ihateregex.io/expr/phone/ and it works for my purpose
const regex = /^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$/gmi
// I expect found to have [+1777654352]
const found = string.match(regex);
Instead, I keep getting null in my found array. I am not sure what I am doing wrong. I hope someone out there can point me in the right direction.
this regex has ^ on the beginning and $ in the end so I'm pretty sure it matches only on phone numbers that are separated by line breaks, or that are alone in their String.
This regex should work for your needs:
let string = "30000 loaves of bread were purchased by +1777654352"
const regex = /[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}/gmi;
const found = string.match(regex);
Silly me!
I was using the regular expression poorly. Using the ^ and $ flags were causing the issue. They signify the start and end of the string and hopefully someone can explain why it behaves that way. Removing those made string.match(regex) work as expected.
This question already has answers here:
Regular expression for a string containing one word but not another
(5 answers)
Closed 1 year ago.
I have some troubles with regex, until now, I had no problem except for the following one :
I have 2 strings, I want to match with one but not the second one which contains a specific word.
var text1 = "The sun is yellow and the sky is blue";
var text2 = "The sun is yellow and the clouds are white";
It's really basic for the example but my regex was like that before :
var regex = /sun/g;
So this was ok for the text1 BUT now I want to return false the match if the string contains "clouds"
So text1 would be TRUE but not text2
I tried with (?!clouds) but I'm probably doing it wrong. It's pretty hard to use regex at this level. So I hope you could help me.
Thank you
Something like this would do it:
^(?!.*\bclouds\b)(?=.*\bsun\b).*$
https://regex101.com/r/TYZHwS/1
This question already has answers here:
Return true/false for a matched/not matched regex
(5 answers)
Closed 3 years ago.
I'm trying to match an entire string against a regex formula. This is for validating if a phone number field is likely correct (just based on allowed characters, anyone can make up an number). I've played with Regex before but never truly understood the nuances that make it powerful.
Below I have my dummy phone number and I have the regex I'm using. As you can see I'm simply comparing the length of the match vs the length of the string and if they match the number must be valid.
Is there a way to get a simple true/false reply from a Regex check on an entire string?
var num = '+1 (888) 456-7896';
var regex = /[0-9+ ()-]*$/;
var found = num.match(regex);
console.log(found[0].length);
console.log(num.length);
You can use test()
var found = regex.test(num);
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.
This question already has answers here:
How do I make the first letter of a string uppercase in JavaScript?
(96 answers)
Closed 7 years ago.
this is a test. this is one more test. and this also new test.
This is test. This is one more test. And this is new test.
You don't need jQuery for this. You can use Javascript string and array functions.
Split the string by ., to separate different sentences
Trim each sentence
Capitalize first letter
Join by .
var str = 'this is a test. this is one more test. and this also new test.';
var newStr = str.split('.').map(function(el) {
el = el.trim();
return el.substr(0, 1).toUpperCase() + el.substr(1);
}).join('. ');
alert(newStr.trim());