doujiao1814 2015-06-19 06:08
浏览 31
已采纳

如果字符串不是javascript中使用正则表达式的较大字符串的一部分,请替换字符串

I'm trying to write a function which would Uppercase some words in a textarea but I'm having trouble, as these words could be part of other words and I only want to uppercase them when they are standing alone.

E.g.: I want to uppercase OR but only when not in a word like befORe. These words can however be written in a multitude of ways, like at the start of a line, between one or more whitespace characters or in something like this: (somethingsomething)OR(somethingelse) and probably even more, depending on the way other people write their query code.

I had the idea of working with regex, but I'm not skilled enough to work out every possibility of writing these words.

I have no way of changing the textarea to other, better plugins or editors as it's already part of a functioning script.

Any helpful idea would be greatly appreciated.

EDIT: One additional question

I got really helpful answers to my question, Thank you everyone. I do have an additional question though. What if, other then checking that it's not part of a word, I also want to check that it's not preceded or followed by a . period. I tried adding (?!\.) at the beginning of it, but that doesN't work at all. Is there a way to check for that as well?

//EDIT: Never mind, I figured it out. it's \b(?!\.)something(?!\.)\b for the check at the start and end of the word.

  • 写回答

3条回答 默认 最新

  • dscjp19831212 2015-06-19 06:29
    关注

    Something like this? ( °_• )

    var txt="or some text or before (.)or(.) anywhere Or";
    
    console.log(txt.replace(/\bor\b/gi,"OR"));     
    
    // "OR some text OR before (.)OR(.) anywhere OR"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器