How to remove following symbol/special character in string using jquery ¶ - javascript

How to remove following symbol/special character in string using jquery
¶
I have problem during comparing two strings .
Above mentioned symbol is append when I compute difference.
I also test it in my local application
please check link below.
http://neil.fraser.name/software/diff_match_patch/svn/trunk/demos/demo_diff.html

var s = "This is a string that contains ¶, a special character.";
s = s.replace(/¶/g, "");
Yields:
"This is a string that contains , a special character."
This will remove all occurrences of the character. No jQuery necessary - just vanilla browser-provided JavaScript.
Some additional details are available at https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace.

jQuery isn't needed for this, you can achieve it by using native javascripts replace function:
var myString = "ABC¶DEFG"
alert(myString.replace("¶", ""))
Example fiddle

javascript replace function should work. If it isn't finding the character then try using the unicode or ansi equivalent.
http://www.alanwood.net/demos/ansi.html

Related

Converting double backslash into backslash used for escaping?

I have a javascript string that contains \\n. When this is displayed out to a webpage, it shows literally as \n (as expected). I used text.replace(/\\n/g, '\n') to get it to act as a newline (which is the desired format).
I'm trying to determine the best way to catch all such instances (including similar instances like tabs \\t -> \t).
Is there a way to use regex (can't determine how to copy the matched wildcard letter to use in the replacement string) or anything else?
As mentioned by dandavis in the comments in original post, JSON.parse() ended up working for me.
i.e. text = JSON.parse(text);
Second answer, the first was wrong.
JavaScript works on a special way in this case. Read this for more details.
In your case it should be one of this ...
var JSCodeNewLine = "\u000A";
text.replace(/\\n/g, JSCodeNewLine);
var JSCodeCarriageReturnNewLine = "\u000D\u000A";
text.replace(/\\n/g, JSCodeCarriageReturnNewLine);

How can i remove http://webservices.rki.dk this in xml

I want to remove namespace in xml.Can you please write regular expression in javascript for the following two strings.I want these two strings in my whole xml.
xmlns="http://webservices.rki.dk"
xmlns="http://webservices.rki.dk/"
You want to use string.replace() to get rid of those. See here.
Example to globally replace in a string (You need to escape some chars to make it work):
var str1 = 'xmlns="http://webservices.rki.dk" hello xmlns="http://webservices.rki.dk"'
var str1Fixed = str1.replace(/xmlns=\"http:\/\/webservices.rki.dk\"/g,"");
alert(str1Fixed);

Regexp - from first character to & character

I have string like this: http://someurl.com?test&lettersg and I would like to mach part from first letter to & (without &, this part only: http://someurl.com?test).
var match = /^[^&]*/.exec("http://someurl.com?test&lettersg")[0];
If its the code you want, look at the source of this JQuery Plugin (URL Parser). Or you can just go ahead and use the plugin.
For URL manipulation I suggest using URI.js.
Your problem can be solved with and without RegExp:
var string = "http://someurl.com?test&lettersg";
console.log(string.match(/^[^&]+/)[0]);
console.log(string.substring(0, string.indexOf('&')));

Omitting special characters from the string using Jquery

I have to implement some type of pixel for analytic and it requires passing a session Id in the url string. My sessionID contains special characters. It looks something like this BFhGlzT6FBkDr2Zndp0!-1309
I need to remove the (-!) characters from this string, how do I achieve this using jquery? I need to make sure jquery remove those characters before it render otherwise it will not report a visit to analytic.
Guys thanks your help but maybe I need to explain bit further, my pixel code look some what like this "
img src="https://sometime.com/png?org_id=k8vif92&session_
id=T6PtTyRSqhGPYBhp84frwth67n6fL7wcLBFhGlzT6FBkDr2Zndp0!-130901808!1319637471144&m=2&m=2" alt="">
Before this pixel fire off, I need to replace the character in the sessionId string to remove !- and keep in mind session id will change every time there is a new session. I need a code that is generic so it works no matter what session id is, it needs to delete special characters from it.
Try using .replace:
var token = "BFhGlzT6FBkDr2Zndp0!-1309";
token.replace(/[^A-Za-z0-9]/g, "");
this will remove any character that's not a letter or number. More concisely:
token.replace(/\W/g, "");
(this won't replace underscores)
Black-listing ! and - (fiddle):
var url = "BFhGlzT6FBkDr2Zndp0!-1309";
document.write(url.replace(/[!\-]/g,""));
White-listing alpha-numeric (fiddle):
var url = "BFhGlzT6FBkDr2Zndp0!-1309";
document.write(url.replace(/[^a-z0-9]/ig,""));
var str = "BFhGlzT6FBkDr2Zndp0!-1309".replace("!-","");
fiddle: http://jsfiddle.net/neilheinrich/eYCjX/
Define a regular expression character set that contains all the allowed characters. For example, yours might look like /[a-zA-Z0-9]/. Now invert it with the complementary character set [^a-zA-Z0-9] and remove all those characters with the String.replace method.
mystring = mystring.replace(/[^a-zA-Z0-9]/g, "");
You dont need jquery for this. You can use javascript regex. See this jsfiddle
var code = "BFhGlzT6FBkDr2Zndp0!-1309";
code = code.replace(/[!-]/g,"");
alert(code);

removing phpbb tag using regex javascript

I'm trying to remove a rectangular brackets(bbcode style) using javascript, this is for removing unwanted bbcode.
I try with this.
theString .replace(/\[quote[^\/]+\]*\[\/quote\]/, "")
it works with this string sample:
theString = "[quote=MyName;225]Test 123[/quote]";
it will fail within this sample:
theString = "[quote=MyName;225]Test [quote]inside quotes[/quote]123[/quote]";
if there any solution beside regex no problem
The other 2 solutions simply do not work (see my comments). To solve this problem you first need to craft a regex which matches the innermost matching quote elements (which contain neither [QUOTE..] nor [/QUOTE]). Next, you need to iterate, applying this regex over and over until there are no more QUOTE elements left. This tested function does what you want:
function filterQuotes(text)
{ // Regex matches inner [QUOTE]non-quote-stuff[/quote] tag.
var re = /\[quote[^\[]+(?:(?!\[\/?quote\b)\[[^\[]*)*\[\/quote\]/ig;
while (text.search(re) !== -1)
{ // Need to iterate removing QUOTEs from inside out.
text = text.replace(re, "");
}
return text;
}
Note that this regex employs Jeffrey Friedl's "Unrolling the loop" efficiency technique and is not only accurate, but is quite fast to boot.
See: Mastering Regular Expressions (3rd Edition) (highly recommended).
Try this one:
/\[quote[^\/]+\].*\[\/quote\]$/
The $ sign indicates that only the closing quote element at the end of the string should be used to determine the ending of the quote you're trying to remove.
And i added a "." before the asterisk so that this will match any sign in between. I tested this with your two strings and it worked.
edit: I don't exactly know how you are using that. But just as an addition. If you want the pattern also to match to a string where no attributes are added for example:
[quote]Hello[/quote]
You should change the "+" sign into an asterisk as well like this:
/\[quote[^\/]*\].*\[\/quote\]$/
This answer has flaws, see Ridgerunner's answer for a more correct one.
Here's my crack at it.
function filterQuotes(text)
{
return text.replace(/\[(\/)?quote([^\/]*)?\]/g,"");
}

Categories

Resources