I am trying to split a string up. I need to somehow take out white space and replace it with _
So for instance:
Jiffy Lube
but I want it to return
Jiffy_lube
Does this require regex? or do I do something like .split('').join('');
Im not really sure any help would be very appreciated! Thank you!
Example:
Dicks Sporting Goods
return:
Dicks_Sporting_Goods
THANK YOU ALL FOR YOUR HELP! IM SORRY THIS IS A POOR QUESTION. I UNDERSTAND NOW WHY ITS A POOR QUESTION. I WILL STILL MARK ANSWERED THOUGH.
Yes, that may sound strange but the easiest way to replace a single character more than once in a string is to use a regular expression.
Use replace :
str = str.replace(/\s/g,'_')
You could also use split and join :
str = str.split(' ').join('_')
but that would be both less direct and slower.
As you said str.split(' ').join('_')
maybe this will help:
var mystr = "Dicks Sporting Goods"
alert(mystr.replace(/\s/g,"_"))
// Dicks_Sporting_Goods
Related
I am totally not confirm with Regular Expressions, so may you guys can help me out.
I have a String like "blablabla_300x300.jpg" where 300 can be any number
I need to replace the "_300x300" with ""
Can please someone provide me the correct answer (Javascript)
Thank you so much
Try this:
var s = "blablabla_300x300.jpg";
s = s.replace(/(_\d+x\d+)(\.jpg)$/, "$2");
console.log(s);
I'm trying to use Regex in javascript. Lets take this scenario,
str = "This; is; John; & Jane;"
The result I need,
str= "This* is* John* & Jane*"
This is what I have tried,
str.replace(/\^(?!&)\w+;\s/g, "*");
Please help.
Thank you.
Try
str.replace(/(^|\s)([^&]\S+?);(?=$|\s)/g, "$1$2*")
You cannot do that without capturing groups because that would require a lookbehind assertion, which Javascript doesn't support.
Try this one:
str.replace(/((^| )[^&]\w+?);/g, "$1*");
I have a phrase like
"everything is changing around me, wonderfull thing+, tthingxx"
and I want to modify every word that contains ***thing at the end of that word, or at most another character after "thing", like "+" or "h" or "x"...
something like
string = 'everything is changing around me, wonderful thing+, tthingxx'
regex = new RegExp('thing(\\+|[g-z])$','g');
string = string.replace(regex, '<b>thing$1</b>');
what I want? everything is changing around me, wonderful thing+, tthingxx
The result of my regexp? anything working... if I remove the $ all the words containing "thing" and at least another character after it are matched:
everything is changing around me, wonderful thing+, tthingxx
I tryed everything but - in first place I can't understand very well technical english - and second I did't find the answer around.
what I have to do??? thanks in advance
the solution I found was using this regular expression
/thing([+g-z]){0,1}\b/g
or with the RegExp (I need it because I have to pass a variable):
myvar = 'thing';
regex = new RegExp(myvar + "([+g-z]){0,1}\\b" , "g");
I was missing the escape \ when doing the regular expression in the second mode. But this isn't enough: the + goes out of the < b > and I don't really know why!!!
the solution that works as I want is the one by #Qtax:
/thing([+g-z])?(?!\w)/g
thank to the community!
To solve the issue with + not matching when using \b you could use (?!\w) instead of \b there, like:
thing[+g-z]?(?!\w)
Use boundary in your regex
\b\w+thing(\+|[g-z])?\b
If I understand what you want, then:
string = 'everything is changing around me, wonderful thing+, tthingxx';
string = string.replace(/thing(\b|[+g-z]$)/g, '<b>thing$1</b>');
...which results in:
every<b>thing</b> is changing around me, wonderful <b>thing</b>+, tthingxx
\b is a word boundary, so what the regular expression says is anywhere it finds "thing" followed by a word boundary or + or g-z at the end of the string, do the replacement.
I have this string here:
<br><br>|Me-Foo|: htht
What i want to do is to transform it to this:
<br><br>Me: htht
basically to change only the part inside the two "|", remembering tha the "Foo" might change with another name, like "john" or whatever.
.. But I don't know how to!? A quick solution anyone?
Thanks
You can remove that with...
str = str.replace(/\|(\w+)-\w+\|/, '$1');
You didn't specify the constraints of what appears between the pipes. If word characters (\w) aren't flexible enough, adjust as required.
You can easily achieve this with combination of indexOf, lastIndexOf, and substring js methods... documentation
It is hard to tell the general case from your example, but let me try:
str = str.replace(/\|([^|]+)-Foo\|/, '$1');
Is this helpful?
theString = theString.replace(/\|(.+)-.+\|/, "$1");
There are several answers given. This one replaces
"anything*& 45|anything8 \$-().?anything| 90fanything"
with
"anything*& 45anything8 \$ 90fanything"
The best answer depends on what could possibly be in between the pipes.
I have a sentence structure along the lines of
[word1]{word2} is going to the [word3]{word4}
I'm trying to use a javascript regex to match the words for replacement later. To do this, I'm working towards getting the following multi-dimensional array:
[["word1", "word2"],["word3","word4"]]
I'm currently using this regex for the job:
\[(.*?)\]\{(.*?)\}
However, it comes up with results like:
["[word1]{word2}", "word1", "word2"]
or worse. I don't really understand why because this regex seems to work in Ruby just fine, and I'm not really much of a regex expert in general to understand what's going on. I'm just curious if there are any javascript rege expert's out there to whom this answer is very clear and can guide me along with what's going on here. I appreciate any help!
Edit:
This is the code I'm using just to test the matching:
function convertText(stringText) {
var regex = /\[(.*?)\]\{(.*?)\}/;
console.log(stringText.match(regex));
}
I assume you are using the exec method of the regular expression.
What you are doing is almost correct. exec returns an array where the first element is the entire match and the remaining elements are the groups. You want only the elements at indexes 1 and 2. Try something like this, but of course store the results into an array instead of using an alert:
var string = '[word1]{word2} is going to the [word3]{word4}';
var pattern = /\[(.*?)\]\{(.*?)\}/g;
var m;
while(m = pattern.exec(string)) {
alert(m[1] + ',' + m[2]);
}
This displays two alerts:
word1,word2
word3,word4
What you're seeing is Japanese hiragana. Make sure your input is in English maybe?
Edited to say: Upon further review, it looks like a dictionary entry in Japanese. The 私 is kanji and the わたし is hiragana, a phonetic pronunciation of the kanji. FWIW, the word is "Watashi" which is one of the words for "I" (oneself) in Japanese.