I have a text box on a HTML page, the idea is that the user can enter any text into the box and then Jquery will remove every other than letters of the alphabet.
This is what I have come up with, it works for small text samples, 1-2 lines, however anything over this it only works on a small section of the input text, is there a better way to do this?
$("#read").change(function () {
read = $("#read").val().toLowerCase().replace(/[^a-z]+/, '');
$("#read").val(read);
});
add /gi to end of your regular expression
/[^a-z]+/gi
then it will replace all occurrences through whole string.
g (for global) and i (for ignore case)
with the comments of #Lunar this is how your code should look like
$("#read").change(function () {
$(this).val($(this).val().replace(/[^a-z]/gi, ''));
});
You need to enable the regex's global flag, so that it will replace all matches instead of just the first match.
/[^a-z]+/ig
More about JS regular expressions.
Related
I am using Sublime Text to write some Javascript and need to do a simple text replacement in the editor in order to set code up. I can do it manually but I figured there must be a way to have the replacement occur automatically with RegEx. I've used RegEx a bunch before but have never used it to grab data from one part of the code to reference and edit another part of the code. For example, I have this:
var example_1 = 836;
var example_2 = 837;
var example_3 = 838;
var example_4 = 846;
And then I have this:
SELECT_122=836
SELECT_143=837
SELECT_144=838
SELECT_145=846
I want these to use the corresponding values and format them like this:
SELECT_122: example_1,
SELECT_143: example_2,
SELECT_144: example_3,
SELECT_145: example_4
Note that I'm updating the equal signs to colons with spaces so I figured doing all these changes could be done with some sort of search and replace. I have a large amount of these so I figured it would be best to learn how to do this if it's possible.
I don't have SublimeText, but you said in a comment that you want to do it through a text editor. Here is what works for me in EditPad Pro, it may work in Sublime.
Search:
(?s)(var (example_\d++) = (\d++).*?SELECT_\d++)=\3
Replace:
\1: \2,
Then I click "Replace". This will replace the first instance (SELECT_122=836) with "SELECT_122: example_1,"
Then I click "Replace Next" multiple times, and the SELECT_ strings are left looking like this:
SELECT_122: example_1,
SELECT_143: example_2,
SELECT_144: example_3,
SELECT_145: example_4,
Is this what you want?
Hope the regex and replacement string at least get you started. :)
My regex is for finding certain words in text, and not words inside elemental text.
REGEXP
RegExp('\\b([^<(.*?)>(.?+)<\/(.*?)>])(' + wregex.join('|') + ')\\b(?=\\W)
EXAMPLE
This is some text that should be looked through
though this text <code>Should not be looked at </code> and this text is ok to
look at
So I'll explain my method of my regex Expression which I am having trouble with
([^<(.*?)>(.?+)<\/(.*?)>]) Do Not match any text that starts with <element> nothing inside here until this </element>
Thats the most important so I've tried multiple methods and not sure if this regex is possible. I don't want to match anything starting with a basic html element tag until the ending tag appears then start over searching.
EDIT
I know that RegEx shouldn't be used to parse HTML this is looking through TEXT
Testing Example HERE
Assuming that the text you are searching over is correctly formed (as in, no tag mismatches) the following regex should work:
^([^<]*<([^>]*)>[^<]*</\2>)*[^<]Your Text
This insures that you text is outside of an open and closed set of tags by matching all open and closed sets before getting to your text.
It won't work for nested tags. Regex is incapable of parsing arbitrarily nested tags.
However, please remember, you should not parse html with regex
Why crum everything in a single regex? It can be as simple as this. Notice that I'm using [^] instead of ., to also match newlines.
string.replace(/<[^]+?<\/[^]+?>/, '').match(/what i really want to find/gi)
And yes, this is prone to breakage, as any regex solution would be.
<script type="text/javascript">
var haystackText = document.getElementById("navigation").innerHTML;
var matchText = 'Subscribe to RSS';
var replacementText = '<ul><li>Some Other Thing Here</li></ul>';
var replaced = haystackText.replace(matchText, replacementText);
document.getElementById("navigation").innerHTML = replaced;
</script>
I'm attempting to try and replace a string of HTML code to be something else. I cannot edit the code directly, so I'm using Javascript to alter the code.
If I use the above method Matching Text on a regular string, such as just 'Subscribe to RSS', I can replace it fine. However, once I try to replace an HTML string, the code 'fails'.
Also, what if the HTML I wish to replace contains line breaks? How would I search for that?
<ul><li>\n</li></ul>
??
What should I be using or doing instead of this? Or am I just missing a small step? I did search around here, but maybe my keywords for the search weren't optimal to find a result that fit my situation...
Edit: Gonna mention, I'm writing this script in the footer of my page, well after the text I wish to replace, so it's not an issue of the script being written before what I want to overwrite to appear. :)
Currently you are using String.replace(substring, replacement) that will search for an exact match of the substring and replace it with the replacement e.g.
"Hello world".replace("world", "Kojichan") => "Hello Kojichan"
The problem with exact matches is that it doesn't allow anything else but exact matches.
To solve the problem, you'll have to start to use regular expressions. When using regular expression you have to be aware of
special characters such as ?, /, and \ that need to escaped \?, \/, \\
multiline mode /regexp/m
global matching if you want to replace more than one instance of the expression /regexp/g
closures for allowing multiple instances of white space \s+ for [1..n] white-space characters and \s* for [0..n] white-space characters.
To use regular expression instead of substring matching you just need to change String.replace("substring", "replacement") to String.replace(/regexp/, "replacement") e.g.
"Hello world".replace(/world/, "Kojichan") => "Hello Kojichan"
From MDN:
Note: If a <div>, <span>, or <noembed> node has a child text node that
includes the characters (&), (<), or (>), innerHTML returns these
characters as &, < and > respectively. Use element.textContent
to get a correct copy of these text nodes' contents.
So since textContent (or innerText) won't get you the HTML, you'd have to modify your search string appropriately.
You can use Regular Expressions.
Recommend to use Regular Expression. Notice that ? and / are special characters in Regular Expression. And for global multi-line matching, you need g and m flags set in the regular expression.
Regular expression matching of HTML (other than plain text) that comes out of a web page is a bad idea and is troublesome to make work cross browser (particularly in IE). The HTML that comes out of a web page does not always look the same as what was put in because some browser reconstitute the HTML and don't actually store what went in. Attributes can change order, quote marks can change or disappear, entities can change, etc...
If you want to modify whole tags, then you should directly access the DOM and operate on the actual objects in the page.
I'm stuck. I tried it with regular expressions, but I guess I'm missing something. I'm working with JavaScript.
I have an input like:
(text [number]) the text that follows...
I want an output like:
[number] the text that follows...
I tried it with substr, but my problem is that I do not know the length of the text or number in the brackets. I guess I need the position of the beginning and ending of the number to work with a regEx.
Have you got an idea?
Regexes are the way to go — using JavaScript’s replace function, you don’t need to fiddle with the position of the number in the string.
Try this:
var geoff = '(text 694) the text that follows...';
var geoff_replaced = geoff.replace(/\([^0-9]* ([0-9]*)\)/, '$1');
# geoff_replaced will be "694 the text that follows...
I don’t do much JavaScript regex stuff, so I totally looked up the above on this guide to JavaScript regexes:
http://www.evolt.org/node/36435
It'd help to have a real example but I made one up...
Text:
(Some text 1234) some more text.
Regex:
^.+?(?<Number>\d+)\)(?<Text>.+)$
Replacement:
${Number}${Text}
Full example:
var fixedText = "(Some text 1234) some more text.".replace(/^.+?(?<Number>\d+)\)(?<Text>.+)$/, "${Number}${Text}");
the regex that matches (text [number]) the text that follows... can be like:
"^\(.*?([0-9]*)\)(.*)$"
or you can just match the beginning (and the ending )) and remove it
"^(\(.*?)[0-9]*(\)).*$"
i've already read all tha articles in here wich touch a similar problem but still don't get any solution working. In my case i wanna wrap each word of a string with a span. The words contain special characters like 'äüö...'
What i am doing at the moment is:
var textWrap = text.replace(/\b([a-zA-Z0-9ßÄÖÜäöüÑñÉéÈèÁáÀàÂâŶĈĉĜĝŷÊêÔôÛûŴŵ-]+)\b/g, "<span>$1</span>");
But what happens is that if the äüñ or whatever NON-Ascii character is at the end or at the beginning it also acts like a boundary. Being within a word these characters do't act as a boundary.
'Ärmelkanal' becomes Ä<span>rmelkanal</span> but should be <span>Ärmelkanal</span>
'Käse'works fine... becomes <span>Käse</span>
'diré' becomes <span>dir</span>é but should be <span>diré</span>
Any advice would be very appreciated. I need to do that on clientside :-( BTW did i mention that i hate regular expressions ;-)
Thank You very much!
The problem is that JavaScript recognizes word boundaries only before/after ASCII letters (and numbers/underscore). Just drop the \b anchors and it should work.
result = subject.replace(/[a-zA-Z0-9ßÄÖÜäöüÑñÉéÈèÁáÀàÂâŶĈĉĜĝŷÊêÔôÛûŴŵ-]+/g, "<span>$&</span>");