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.
Related
I want to replace all occurences of .digit with 0.digit.
I'm new to regular expressions but as far as I understand I could use look behind to do this. But JS does not support that, I'd like to know if someone knows a solution.
To show the problem I wrote the following code.
str = "0.11blabla.22bla0.33bla.33"
allow = "\\.\\d*"
str.match(new RegExp(allow,"g"))
[".11", ".22", ".33", ".33"]
deny = "0\\.\\d*"
str.match(new RegExp(deny,"g"))
["0.11", "0.33"]
diffreg= new RegExp("(?!"+deny+")"+allow,"g") // translates to: /(?!0\.\d*)\.\d*/g
str.match(diffreg)
[".11", ".22", ".33", ".33"]
Obviously allow matches all decimal values whereas deny matches all values with a preceding 0. The result should of course be the set difference between the two: [".33", ".33"].
Use a group match.
> str.replace(/([^0])(\.\d)/g, "$10$2");
"0.11blabla0.22bla0.33bla0.33"
I think you are looking for this regex instead
[0]?(\.\d*)
So in your code you will have:
intersectionreg = new RegExp("[0]?("+allow+")","g")
Thanks #richard, edited
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
I am manipulating some content that I receive from an API. At the end of the main text field, The api sometimes returns the string below:
##canvas-link##{"type":"doc","fileName":"xyzv2.jpg","fileExt":"jpg",
"fileSize":"232352",
"file":"405957767101","downloadUrl":"dummytext"}
What is the best way to remove this string from the main text field?
If you're sure it's at the end, this version is fastest;
s.substring(0, s.lastIndexOf("##canvas-link##")) ; // FASTER than most
but updated Just for fun, I've another variant using slice() which is slightly faster in this test even than substring().
s.slice(0,s.lastIndexOf("##canvas-link##")); // FASTEST
Here's a jsPerf which shows them beating both RegEx and split. Although I'm surprised split wasn't quicker.
However, your mileage may vary, and for more complex scenarios I'd expect RegEx (replace) to come out on top.
str = str.replace(/##canvas-link##.*/, '');
Improving Joseph Silbers,
str = str.replace(/##canvas-link##{.*}/, '');
To make sure it doesn't remove anything after.
You could use a regular expression, but a split() will avoid complications with carriage returns.
var str = 'foobar##canvas-link##{"type":"doc","fileName":"xyzv2.jpg","fileExt":"jpg",
"fileSize":"232352",
"file":"405957767101","downloadUrl":"dummytext"}';
var data = str.split('##canvas-link##')[0];
console.log(data);
>> foobar
I've a string done like this: "http://something.org/dom/My_happy_dog_%28is%29cool!"
How can I remove all the initial domain, the multiple underscore and the percentage stuff?
For now I'm just doing some multiple replace, like
str = str.replace("http://something.org/dom/","");
str = str.replace("_%28"," ");
and go on, but it's really ugly.. any help?
Thanks!
EDIT:
the exact input would be "My happy dog is cool!" so I would like to get rid of the initial address and remove the underscores and percentage and put the spaces in the right place!
The problem is that trying to put a regex on Chrome "something goes wrong". Is it a problem of Chrome or my regex?
I'd suggest:
var str = "http://something.org/dom/My_happy_dog_%28is%29cool!";
str.substring(str.lastIndexOf('/')+1).replace(/(_)|(%\d{2,})/g,' ');
JS Fiddle demo.
The reason I took this approach is that RegEx is fairly expensive, and is often tricky to fine tune to the point where edge-cases become less troublesome; so I opted to use simple string manipulation to reduce the RegEx work.
Effectively the above creates a substring of the given str variable, from the index point of the lastIndexOf('/') (which does exactly what you'd expect) and adding 1 to that so the substring is from the point after the / not before it.
The regex: (_) matches the underscores, the | just serves as an or operator and the (%\d{2,}) serves to match digit characters that occur twice in succession and follow a % sign.
The parentheses surrounding each part of the regex around the |, serve to identify matching groups, which are used to identify what parts should be replaced by the ' ' (single-space) string in the second of the arguments passed to replace().
References:
lastIndexOf().
replace().
substring().
You can use unescape to decode the percentages:
str = unescape("http://something.org/dom/My_happy_dog_%28is%29cool!")
str = str.replace("http://something.org/dom/","");
Maybe you could use a regular expression to pull out what you need, rather than getting rid of what you don't want. What is it you are trying to keep?
You can also chain them together as in:
str.replace("http://something.org/dom/", "").replace("something else", "");
You haven't defined the problem very exactly. To get rid of all stretches of characters ending in %<digit><digit> you'd say
var re = /.*%\d\d/g;
var str = str.replace(re, "");
ok, if you want to replace all that stuff I think that you would need something like this:
/(http:\/\/.*\.[a-z]{3}\/.*\/)|(\%[a-z0-9][a-z0-9])|_/g
test
var string = "http://something.org/dom/My_happy_dog_%28is%29cool!";
string = string.replace(/(http:\/\/.*\.[a-z]{3}\/.*\/)|(\%[a-z0-9][a-z0-9])|_/g,"");
I need to capture the price out of the following string:
Price: 30.
I need the 30 here, so I figured I'd use the following regex:
([0-9]+)$
This works in Rubular, but it returns null when I try it in my javascript.
console.log(values[1]);
// Price: 100
var price = values[1].match('/([0-9]+)$/g');
// null
Any ideas? Thanks in advance
Try this:
var price = values[1].match(/([0-9]+)$/g);
JavaScript supports RegExp literals, you don't need quotes and delimiters.
.match(/\d+$/) should behave the same, by the way.
See also: MDN - Creating a Regular Expression
Keep in mind there are simpler ways of getting this data. For example:
var tokens = values[1].split(': ');
var price = tokens[1];
You can also split by a single space, and probably want to add some validation.
Why don't you use this?
var matches = a.match(/\d+/);
then you can consume the first element (or last)
my suggestion is to avoid using $ in the end because there might be a space in the end.
This also works:
var price = values[1].match('([0-9]+)$');
It appears that you escaped the open-perens and therefore the regex is looking for "(90".
You don't need to put quotes around the regular expression in JavaScript.