LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-27-2013, 06:20 AM   #1
cin_
Member
 
Registered: Dec 2010
Posts: 266

Rep: Reputation: 23
Javascript :: live replace() with regex, only when a word 'terminates'


i want to replace every occurrence of a word in a string as it is typed,

here's what i've got taken out of context...
Code:
$(document).on('keyup','textarea', function() {
    this.value=this.value.replace(/\bnot\b$/gi,"_-Ni!-_ ");
});
so this will replace the word 'not' every time you type it into a textarea,

i used the regex to avoid replacing 'not' in words like 'another',

the problem i am having now is that the function replaces words that start with not if you are a slower typer than the script running,

so words like 'noteworthy', get changed before one can type the 'eworthy',

i thought to specify each possible ending word scenario, such as new lines, spaces, punctuation et al, but i instead looked if there was an easier way and was unable to find one, so before i tackd a slue of special characters onto the back of the word i thought i'd ask the community if they knew a way

Last edited by cin_; 07-27-2013 at 06:21 AM. Reason: gramm`err
 
Old 07-27-2013, 07:16 AM   #2
cin_
Member
 
Registered: Dec 2010
Posts: 266

Original Poster
Rep: Reputation: 23
Figured

got some help from the stacks ...
i'll put the solution here for posterity :

Code:
this.value=this.value.replace(/(^|[^a-z])no(?=[^a-z])/gi,"$1_-Ni!-_");  
//let's talk regex :: 
	//^ beginning of input, 
	// | or, 
	// [^a-z] match everything but a-z, 
	// no is the word, 
	// (?=[^a-z]) only match if equal to characters other than a-z,
	// /g global, 
	// i ignore case
	// $1 places last character into this position

... http://stackoverflow.com/questions/1...ord-terminates

Last edited by cin_; 07-27-2013 at 07:20 AM. Reason: gramm`err
 
Old 07-27-2013, 04:31 PM   #3
Doc CPU
Senior Member
 
Registered: Jun 2011
Location: Stuttgart, Germany
Distribution: Mint, Debian, Gentoo, Win 2k/XP
Posts: 1,058

Rep: Reputation: 314Reputation: 314Reputation: 314Reputation: 314
Hi there,

Quote:
Originally Posted by cin_ View Post
got some help from the stacks ...
i'll put the solution here for posterity :

Code:
this.value=this.value.replace(/(^|[^a-z])no(?=[^a-z])/gi,"$1_-Ni!-_");
that's very deformed by simplification. Specifically, you only treat [a-z]/i as letters that make up a word. What about German, Turkish or Finnish umlauts, what about French or Spanish accented letters, what about Greek or Cyrillic letters? And that's just European scripts, just the tip of the iceberg, there are many more characters that should be considered part of a word.

[X] Doc CPU
 
Old 07-28-2013, 04:51 PM   #4
cin_
Member
 
Registered: Dec 2010
Posts: 266

Original Poster
Rep: Reputation: 23
ducky

I totally hear you,
i am aware of the limitations,
but the question was only to replace the word 'not' so the a-z limitation is appropriate

but yes,
in regard to all other languages some more sophisticated, obfuscated?, regex would be required...

some unicode(i) attention is necessary, if you want to avoid just trying to cover all of your bases outright


(i) http://unicode.org/reports/tr18/

Last edited by cin_; 07-28-2013 at 04:54 PM. Reason: gramm`err
 
Old 07-29-2013, 03:29 PM   #5
Doc CPU
Senior Member
 
Registered: Jun 2011
Location: Stuttgart, Germany
Distribution: Mint, Debian, Gentoo, Win 2k/XP
Posts: 1,058

Rep: Reputation: 314Reputation: 314Reputation: 314Reputation: 314
Hi there,

Quote:
Originally Posted by cin_ View Post
i am aware of the limitations,
but the question was only to replace the word 'not' so the a-z limitation is appropriate
alright, I thought that this was just an arbitrary placeholder, and that you were in fact searching for a more general solution.

[X] Doc CPU
 
  


Reply

Tags
javascript, jquery, regex


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Find/Replace shell script that replaces word with other word in text, filenames yanom Programming 8 09-12-2012 01:29 AM
[SOLVED] differences between shell regex and php regex and perl regex and javascript and mysql golden_boy615 Linux - General 2 04-19-2011 02:10 AM
[SOLVED] Regex: word^ matches what? lesca Programming 9 02-07-2011 04:29 PM
[SOLVED] Simple regex match without replace? nrg Programming 1 11-21-2009 04:04 AM
PHP regex replace? iluvatar Programming 5 09-10-2004 11:03 AM


All times are GMT -5. The time now is 07:50 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration