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
Related
This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 4 years ago.
Suppose we have a string: "Someone one one-way"
And I want to replace THE WORD "one" with THE WORD "two".
Note: I don't want "someone" or "one-way" to change.
I tried to use str.replace(/one/g , "two");
But the result was "Sometwo two two-way"
This is not the intended result...
I want to get "Someone two one-way"
ANY HELP WOULD BE APPRECIATED. THANKS IN ADVANCE....
You can validate the spaces too:
//var str = "Someone one one-way";
var str = "one one Someone one one-way one,one";
var replace='two';
//using lookahead and lookbehind
var result= str.replace(/(?<=[^a-zA-Z-]|^)one(?=[^a-zA-Z-]|$)/gm , "two");
console.log(result);
//result: two two Someone two one-way two,two
eval('STR.SPLIT(" ONE ").JOIN(" TWO ")'.toLowerCase())
try this regex:
\s(one)\s
you may add modify it for case sensitive.
let input = "Someone one one-way"
output = input.split(" ").map((item)=> item ==='one'? 'two' : item).join(" ");
console.log(output)
You can try this:
.replace(/\bone\b(?!-)/g , "two")
\b matches on a word boundary.
(?!-) is a negative lookahead because you do not want to replace one if followed by a dash. \b recognizes the dash as a word boundary.
There is another pitfall when you have something like "-one".
Use this if this is a Problem:
.replace(/[^-]\bone\b(?!-)/g , function(x){return x.substring(0,1) + 'two'})
Ideally you would use negative look ahead, but this is currently not supported by all Browsers.
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 answers here:
Replace a Regex capture group with uppercase in Javascript
(7 answers)
Closed 5 years ago.
I wonder if there is a way to make a string uppercase using regex only in JS.
The thing is that I am giving my users a string transformation system.
The user supply me with three parameters : original text, replace regex, subtitution regex.
for example:
original : 'stackoverflow'
replace : /([a-z])(.*)/g
subtitution : $1
Result : 's'
I want to give them the abilitty to set the entire string to uppercase. I've noticed in some other SO questions that there are systems that allows that. for example in sublime text you can do '/\U$1/' to set the entire string to uppercase.
Notice: I cannot use toUpperCase or toLowerCase in any way
Javascript has an inbuilt uppercasing method
var str = "Hello World!";
var res = str.toUpperCase();
The result of res will be:
HELLO WORLD!
This question already has answers here:
How do I split a string, breaking at a particular character?
(17 answers)
Closed 7 years ago.
I want to split a string in Java script using
str.split([separator[, limit]])
I want to split it by a space, but ' ' did not work.
What should I use?
This question has previously been asked here: How do I split a string, breaking at a particular character?
In your specific case, based on what you provided, it appears you are attempting to split on nothing. '' instead of an actual space ' '.
To replace the spaces by commas:
var str = "How are you doing today?";
var res = str.split(" ");
//Output: How,are,you,doing,today?
Like described here: http://www.w3schools.com/jsref/jsref_split.asp
Another option is to use str.replace:
Var str = "Mr Blue has a blue house and a blue car";
var res = str.replace(/blue/gi, "red");
//Output: Mr red has a red house and a red car
Like described here: http://www.w3schools.com/jsref/jsref_replace.asp
But what you may actually wanted:
var str = "Test[separator[, limit]]Test";
var res = str.split("[separator[, limit]]").join(" ");
//Output: Test Test
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Regular Expression to find a string included between two characters, while EXCLUDING the delimiters
I have a function where I have to get text which is enclosed in square brackets but not brackets for example
this is [test] line i [want] text [inside] square [brackets]
from the above line I want words:
test
want
inside
brackets
I am trying with to do this with /\[(.*?)\]/g but I am not getting satisfied result, I get the words inside brackets but also brackets which are not what I want
I did search for some similar type of question on SO but none of those solution work properly for me here is one what found (?<=\[)[^]]+(?=\]) this works in RegEx coach but not with JavaScript. Here is reference from where I got this
here is what I have done so far: demo
please help.
A single lookahead should do the trick here:
a = "this is [test] line i [want] text [inside] square [brackets]"
words = a.match(/[^[\]]+(?=])/g)
but in a general case, exec or replace-based loops lead to simpler code:
words = []
a.replace(/\[(.+?)\]/g, function($0, $1) { words.push($1) })
This fiddle uses RegExp.exec and outputs only what's inside the parenthesis.
var data = "this is [test] line i [want] text [inside] square [brackets]"
var re= /\[(.*?)\]/g;
for(m = re.exec(data); m; m = re.exec(data)){
alert(m[1])
}