Replace a specific set of characters using JavaScript only - javascript

I have an RTF string that contains \*\revtbl{Unknown;}}, it is used to indecate that the word that follows is misspelled (I think) and I need to remove it, when I do:
.replace(/{\*\revtbl{Unknown;}}/g, "")
I get two lines:
*
evtbl{Unknown;}}
When I change to:
.replace(/{\*\r|evtbl{Unknown;}}/g, "")
I get just the * and a second line. e.g.:
var tt = '\*\revtbl{Unknown;}}';
tt=tt.replace(/{\*\r|evtbl{Unknown;}}/g, "");
alert('"'+tt+'"');
I see:
"*
"
I could not find any reference about the use of the pipe | character so I need to know: if by using it am I asking to replace two separate strings one being {\*\r and the other being evtbl{Unknown;}} bottom line I need to replace the literal string \*\revtbl{Unknown;}} with nothing.

I think it is just a matter of escaping all the characters correctly
//sample string - NOTE: the backslashes had to be escaped for JS
var str = "RTF string that contains \\*\\revtbl{Unknown;}}, it is used to indecate that the word that follows is misspelled (I think) and I need to remove it, when I do:";
var regEx = /\\\*\\revtbl\{Unknown;\}\}/g;
console.log(str.replace(regEx, ''));

Related

remove some text after - from string

I want to remove some text from string or integer using javascipt or jquery..
I have string "flow-[s/c]", "flow-max[s/c]","flow-min[s/c]", "Usage-[s/c]", "temperature"
And I want for each :
"flow", "flow-max","flow-min", "Usage", "temperature"
As you can see. I want to remove all the data after - found expect flow-max and flow-min
What I am doing :
var legendName = $(textElement).text().toLowerCase().replace(" ", "-");
Taking the legend Name example : "flow-[s/c]", "flow-max[s/c]"
var displayVal = legendName.split('-')[0];
remove all the data after - found
But I am not able to add condition for flow-max because at this case I will be having two - and two place like flow-min-[s/c]
var displayVal = $(textElement).text().replace(/\-?\[s\/c\]/, "");
The code /\-?\[s\/c\]/ is a regular expression, where:
/ at the start and end are the delimiters of the expression.
\ is an escape character, indicating that the following character should be taken literally (in our example we need it in front of -, [, / and ] because those are control character for regular expressions).
? means the previous character is optional.
So it replaces an optional dash (-) followed by the text [s/c], with an empty string.
Just use this simple regex /(max|min)\[.*?]|-\[.*?]/g. The regex is simple, if you see what it does separately.
The logic has been separated by | operator.
legendName = legendName.replace(/(max|min)\[.*?]|-\[.*?]/, "$1");
You can use lastoccur = legendName.lastIndexOf("-"); to find the last occur of "-" and then split your string.
Reference: http://www.w3schools.com/jsref/jsref_lastindexof.asp

jQuery - Add a comma only if a there is a value

I can’t quite figure out the best way to do this but I have a series of values that are being fed into my jquery plugin. I’m following the convention of adding a space after each value. I then grab the jquery parameters and attempt to add a ‘,’ to each one so that it can be feed into a json call. The problem is that if I add a ‘,’ in place of the space and the value returned from the variable in the plugin is empty I get multiple commas that I don’t need. (i.e. ,,podcast). How would I only add a comma if there is a value in my variable?
$(document).ready(function () {
$('#gallery').mediaFilters({
mediaType: '${article} ${podcast} ${image}',
headerType: '${internet} ${cloud} ${background}',
});
});
JQUERY to send to JSON:
var mediaType = "&mediaType=" + opts.mediaType.split(/[ ]/).join(',');
var headerType = "&headerType=" + opts.headerType.split(/[ ]/).join(',');
The regex / +/ will match multiple spaces, so the input a b will turn into a,b.
The regex /^ *| *$/ will match the spaces at the beginning and end of the string.
Together, here is code that will replace those with a comma, and remove the beginning and end spaces.
var mediaType = "&mediaType=" + opts.mediaType.replace(/^ *| *$/g,'').replace(/ +/g,',');
var headerType = "&headerType=" + opts.headerType.replace(/^ *| *$/g,'').replace(/ +/g,',');

How to remove the special characters from a string using javascript

I have the below String value to be displayed in text area and i want to remove the first characters ##*n|n from the string .
The string is as follows :
Symbol-001
##*n|nClaimant Name
##*n|nTransaction
I have used the below code to deal with removing the special characters
var paramVal1 = parent.noteText; //paramVal1 will have the string now
var pattern = /[##*n|n]/g;
var paramVal1 = paramVal1.replace(pattern,'');
document.getElementById("txtNoteArea").value = paramval1;//appending the refined string to text area
For the above used code am getting the out put string as below
Symbol-001
|Claimat Name //here 'n' is missing and i have an extra '|' character
|Transactio //'n' is missing here too and an extra '|' character
Kindly help to remove the characters ##*n|n without affecting the other values
What your regex is saying is "remove any of the following characters: #|*n". Clearly this isn't what you want!
Try this instead: /##\*n\|n/g
This says "remove the literal string ##*n|n". The backslashes remove the special meaning from * and |.
You are using regular expression reserved chars in your pattern, you need to escape them
You can use this expression:
var pattern = /[\#\#\*n\|n]/g;
i think use this /[##*n\|n]/g regEx
If you want to replace the first occurrence as you say on your question, you don't need to use regex. A simple string will do, as long as you escape the asterisk:
var str = "Symbol-001 ##*n|nClaimant Name ##*n|nTransaction";
var str2 = str.replace("##\*n|n", ""); //output: "Symbol-001 Claimant Name ##*n|nTransaction"
If you want to replace all the occurrences, you can use regex, escaping all the characters that have a special meaning:
var str3 = str.replace(/\#\#\*n\|n/g, ""); //output: "Symbol-001 Claimant Name Transaction"
Have a look at this regex builder, might come in handy - http://gskinner.com/RegExr/

Finding image url via using Regex

Any working Regex to find image url ?
Example :
var reg = /^url\(|url\(".*"\)|\)$/;
var string = 'url("http://domain.com/randompath/random4509324041123213.jpg")';
var string2 = 'url(http://domain.com/randompath/random4509324041123213.jpg)';
console.log(string.match(reg));
console.log(string2.match(reg));
I tied but fail with this reg
pattern will look like this, I just want image url between url(" ") or url( )
I just want to get output like http://domain.com/randompath/random4509324041123213.jpg
http://jsbin.com/ahewaq/1/edit
I'd simply use this expression:
/url.*\("?([^")]+)/
This returns an array, where the first index (0) contains the entire match, the second will be the url itself, like so:
'url("http://domain.com/randompath/random4509324041123213.jpg")'.match(/url.*\("?([^")]+)/)[1];
//returns "http://domain.com/randompath/random4509324041123213.jpg"
//or without the quotes, same return, same expression
'url(http://domain.com/randompath/random4509324041123213.jpg)'.match(/url.*\("?([^")]+)/)[1];
If there is a change that single and double quotes are used, you can simply replace all " by either '" or ['"], in this case:
/url.*\(["']?([^"')]+)/
Try this regexp:
var regex = /\burl\(\"?(.*?)\"?\)/;
var match = regex.exec(string);
console.log(match[1]);
The URL is captured in the first subgroup.
If the string will always be consistent, one option would be simply to remove the first 4 characters url(" and the last two "):
var string = 'url("http://domain.com/randompath/random4509324041123213.jpg")';
// Remove last two characters
string = string.substr(0, string.length - 2);
// Remove first five characters
string = string.substr(5, string.length);
Here's a working fiddle.
Benefit of this approach: You can edit it yourself, without asking StackOverflow to do it for you. RegEx is great, but if you don't know it, peppering your code with it makes for a frustrating refactor.

Javascript match last #<User>

I'm trying to make an auto-complete function for twitter usernames.
So far, I have the following code:
function OnKeyUp(txtboxid){
var text = $('#'+txtboxid).val()
var regex = '(^|\s)#(\w*[a-zA-Z_]+\w*)'
var results = text.match(RegExp(regex, 'gm'))
console.debug(results)
}
The problem is, it matches only text when it is at the beginning of the string (eg: #yser)
What i want is a regex that can mach such a string like this "hello #user2 , #user and #user3 how are you"
I'm not sure how to accomplish this.
Searched google for about 3 hours now and still nothing found.
Also, it would be great to only the the last username when its changed.
Your regex is fine. The only problem is that backslashes in the string will be removed or replaced when the string is parsed, instead of being interpreted by the regular expression parser. You need to re-escape each of them with an extra backslash:
var regex = '(^|\\s)#(\\w*[a-zA-Z_]+\\w*)';
Instead of specifying the regular expression with a string and the RegEx function, you should usually use a regular expression literal. It's delimited by backslashes instead of double-quotes, with the flags appended to the end:
var results = text.match(/(^|\s)#(\w*[a-zA-Z_]+\w*)/gm);

Categories