Remove whitespace after a particular symbol - javascript

I've got a string which has the > symbol multiple times. After every > symbol there is a line break. How can I remove whitespaces after every > symbol in the string?
This is what I tried for spaces. But I need to remove whitespaces only after > symbol.
str.replace(/\s/g, '');
String:
<Apple is red>
<Grapes - Purple>
<Strawberries are Red>

If you are trying to remove newline characters you can use RegExp /(>)(\n)/g to replace second capture group (\n) with replacement empty string ""
var str = `<Apple is red>
<Grapes - Purple>
<Strawberries are Red>`;
console.log(`str:${str}`);
var res = str.replace(/(>)(\n)/g, "$1");
console.log(`res:${res}`);

Try this:
str.replace(/>\s/g, '>')
Demo:
console.log(`<Apple is red>
<Grapes - Purple>
<Strawberries are Red>`.replace(/>\s/g, '>'))

Use the following approach:
var str = 'some> text > the end>',
replaced = str.replace(/(\>)\s+/g, "$1");
console.log(replaced);

You must use /m flag on your regex pattern
You must use $1 capture group symbol on your replacement text
var text = '<Apple is red>\r\n<Grapes - Purple>\r\n<Strawberries are Red>';
var regex = /(>)\s*[\n]/gm;
var strippedText = text.replace(regex,'$1');

Related

javascript replace all

I have a sting like:
var tmp='Hello
I am
( Peter';
I want to replace all words with this pattern &...; to '';
How I can change the code below to make it work:
tmp=String(tmp).replace(/
/g, "");
Thank you.
Update regex with \d{2} instead of the 10 to match any two digit numbers.
var tmp = 'Hello
I am
( Peter';
tmp = String(tmp).replace(/&#\d{2};/g, "");
console.log(tmp);
try this /&#(\d+)(;)/g .Its match the numbers upto reach of ; end
Normal Regex
var tmp='Hello
I am
( Peter';
console.log(tmp.replace(/&#(\d+)(;)/g,""))
Remove the Empty space also use: /(\s+)&#(\d+)(;)/g .\s+ match the empty space before the pattern
Empty space match
var tmp='Hello
I am
( Peter';
console.log(tmp.replace(/(\s+)&#(\d+)(;)/g,""))

How do I replace all spaces, commas and periods in a variable using javascript

I have tried var res = str.replace(/ |,|.|/g, ""); and var res = str.replace(/ |,|.|/gi, "");. What am I missing here?
var str = "Text with comma, space, and period.";
var res = str.replace(/ |,|.|/g, "");
document.write(res);
If you just want to delete all spaces, commas and periods you can do it like that:
var res = str.replace(/[ ,.]/g, "");
You can also use the | operator, but in that case you have to escape the period, because a plain period will match with any character. As a general remark, if in a regular expression you have multiple alternatives with | that all consist of a single character, it is preferable to use a set with [...].
You need to escape dot \.:
"Text with comma, space and period.".replace(/ |,|\.|/g, "")
You can use these lines:
str = str.replace(/[ ,.]/g,'');
Plus i have added a fiddle for this at Fiddle

JavaScript regex to replace a whole word

I have a variable:
var str = "#devtest11 #devtest1";
I use this way to replace #devtest1 with another string:
str.replace(new RegExp('#devtest1', 'g'), "aaaa")
However, its result (aaaa1 aaaa) is not what I expect. The expected result is: #devtest11 aaaa. I just want to replace the whole word #devtest1.
How can I do that?
Use the \b zero-width word-boundary assertion.
var str = "#devtest11 #devtest1";
str.replace(/#devtest1\b/g, "aaaa");
// => #devtest11 aaaa
If you need to also prevent matching the cases like hello#devtest1, you can do this:
var str = "#devtest1 #devtest11 #devtest1 hello#devtest1";
str.replace(/( |^)#devtest1\b/g, "$1aaaa");
// => #devtest11 aaaa
Use word boundary \b for limiting the search to words.
Because # is special character, you need to match it outside of the word.
\b assert position at a word boundary (^\w|\w$|\W\w|\w\W), since \b does not include special characters.
var str = "#devtest11 #devtest1";
str = str.replace(/#devtest1\b/g, "aaaa");
document.write(str);
If your string always starts with # and you don't want other characters to match
var str = "#devtest11 #devtest1";
str = str.replace(/(\s*)#devtest1\b/g, "$1aaaa");
// ^^^^^ ^^
document.write(str);
\b won't work properly if the words are surrounded by non space characters..I suggest the below method
var output=str.replace('(\s|^)#devtest1(?=\s|$)','$1aaaa');

Separating Id from string by using regex

how can I get the first part of ID from the following string?
sText ='DefId #AnyId #MyId';
sText = sText.replace(/ #.*$/g, '');
alert(sText);
The result should be as follows: "DefId #AnyId "
Many thanks in advance.
var sText ='DefId #AnyId #MyId';
var matches = sText.match(/(DefId #.*) #.*/);
if(matches && matches.length > 0) {
alert(matches[1]);
}
Move the grouping parenthesis right 1 character if you also want the space after the first ID. This assumes that the IDs won't contain a #.
You could use this regex,
(.*)#.*?$
DEMO
JS code,
> var sText ='DefId #AnyId #MyId';
undefined
> sText = sText.replace(/(.*)#.*?$/g, '$1');
'DefId #AnyId '
If you don't want any space after #AnyId, then run the below regex to remove that space.
> sText = sText.replace(/(.*) #.*?$/g, '$1');
'DefId #AnyId'
If you need to get rig of everything after last # in the string, use:
sText.replace(/#[^#]+$/, '');

How to strip last element of dash delimited string in Javascript

I have string delimited with dashes like:
x#-ls-foobar-takemeoff-
How can I remove takemeoff- using javascript where takemeoff- can be any amount of characters ending in a dash?
var str = "x#-ls-foobar-takemeoff-";
var newStr = str.replace(/[^-]+-$/,"");
Basic regular expression says
[^-]+ <-- Match any characters that is not a dash
- <-- Match a dash character
$ <-- Match the end of a string
If you have a string str, you can do the following:
str = str.substr(0, str.lastIndexOf("-", str.length - 2));
Using substr() and lastIndexOf():
var myStr = "x#-ls-foobar-takemeoff-";
myStr = myStr.substr(0, myStr.length-1); // remove the trailing -
var lastDash = myStr.lastIndexOf('-'); // find the last -
myStr = myStr.substr(0, lastDash);
alert(myStr);
Outputs:
x#-ls-foobar
jsFiddle here.

Categories

Resources