问题
I am creating a web text editor that uses a new language I created (like BBcode). This markup language will be in Arabic but I am facing these problems :
- It's really hard to mix English and Arabic text in the same line in all IDE's and editors, because strange things happen (the order of words and characters changes)
- when replacing a text with javascript using a pattern the Arabic text doesn't appear
(eg:
"1 text arabic here 1".replace(/1(*)1/,"($1)")
I get empty () )
How can I fix this ?
回答1:
Perhaps you can use some unlikely deliminator character to specify points in your data buffers where your encoding switches. This would let your parsing functions identify chunks of your files that have to be treated differently.
I don't know if your arabic text uses UTF-8 characters. Hopefully it is possible to render all necessary characters in both english and arabic in UTF-8. Then you can use your delimiter to tell your other code how to handle pieces of text that behave differently.
>some english/L-T-R markup
Ⓐ**...markup R-T-L Some**كل الخارجي للنص أو شكل توضع الفقرات في الصفحة التي يقرأها. ولذلك يتم استخدام طريقة لوريم إيبسوم لأنها تعطي توزيعاَ طبيعياَ -إلى حد ما- للأحرف عوضاً عن استخدام "هنا يوجد محتوى نصي، هنا يوجد محتوى نصي" فتجعلها تبدو (أي الأحرف) وكأنها نص مقروء. العديد من برامح النشر المكتبي وبرامح تحرير صفحات الويب تستخدم لوريم إيبسوم بشكل إفتراضي كنموذج عن اⒶ
Dunno about the regex, hopefully there are libraries on github that handle mixed arabic and english regex already :)
edit: Stack Overflow's markdown ate some stuff the first time (and god knows what it did to my pasted arabic lorem ipsum)
edit: here's a start to find the arabic tags
var src = "try to write javascript that replaces <رابط>نص تجريبي</رابط> by <a href='#'>نص تجريبي</a> and you wil notice all the difficulties – user2080105 15 mins ago"
var a = src.match(/try/);
console.log(a);
var b = src.match(/<[^>]*>/);
console.log(b);
var c = src.match(/<\/?[^>]*>/g);
console.log(c);
//Output in node v0.9.4-pre
[ 'try',
index: 0,
input: 'try to write javascript that replaces <رابط>نص تجريبي</رابط> by <a href=\'#\'>نص تجريبي</a> and you wil notice all the difficulties – user2080105 15 mins ago' ]
[ '<رابط>',
index: 38,
input: 'try to write javascript that replaces <رابط>نص تجريبي</رابط> by <a href=\'#\'>نص تجريبي</a> and you wil notice all the difficulties – user2080105 15 mins ago' ]
[ '<رابط>', '</رابط>', '<a href=\'#\'>', '</a>' ]
来源:https://stackoverflow.com/questions/15207486/right-to-left-languages-and-programming-problems