Regex : Replace ; from word if word does not start with & - javascript

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*");

Related

RegEx Ignore Case

I've been trying to create a regex which would ignore the casing.
This is the regex i am trying to use:
/^[A-Za-z0-9._+\-\']+#+test.com$/;
So basically i would want to match any of these
abc#Test.com
abc#TEST.com
abc#teSt.com
I tried this, but it doesn't work:
/^[A-Za-z0-9._+\-\']+#+(?i)+test.com$/;
I read somewhere about the use of (?i), but couldn't find any examples which show their usage in regex to ignore casing.
Thoughts anyone ? Thanks a lot in advance.
Flags go at the end.
/regex/i
i is for case-Insensitive (or ignore-case)
For anyone else who arrives here looking for this, if you've got code that is using the RegExp constructor you can also do this by specifying flags as a second argument:
new RegExp(pattern[, flags])
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/RegExp
For example:
var pattern = /^[A-Za-z0-9._+\-\']+#+test.com$/;
var regExp = new RegExp(pattern, "i");
Simple and straightforward, use following regex expression.
(?i)^[A-Za-z0-9._\-\']+#test.com$

Add _ when there is a space

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

javascript regexp "subword" replace

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.

Complicated Javascript string modify

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.

Special character checking in JavaScript

I have a string:
var string = "asdasASFASDŞGFSD123435489()/%&&&&&&&&&&&&&&&123435";
I want this string to have a space after any group of ampersands. So in this example, I want the output:
"asdasASFASDŞGFSD123435489()/%&&&&&&&&&&&&&&& 123435";
How can I accomplish this in JavaScript?
well, mine is cooler ;)
var string = "asdasASFASDŞGFSD123435489()/%&&&&&&&&&&&&&&&123435";
alert(string.replace(/&+/g, "$& "))
I think this is the job of regular expressions. For example you could do something like this (not tested, just to get the idea):
string.replace("(&+)", "$1 ");

Categories